# Board index » delphi » Help with a random number generator...

## Help with a random number generator...

Hi everyone.  I am a C++ programmer and this is my first shot at a Pascal
program.  I need a random number generator to produce integers in a range
0..k.  In C++, I would usually write the following:

#include <time.h>
using namespace std;
...
srand((unsigned) time (NULL));  //seeds rng to the time
...
int i = rand(); //rand function returns a random number 0..32767

What are the Pascal equivalents to the seed (srand) function and the rand
function?  Is the process of finding a random number different?  Any help is
appreciated.  Thanks!!!

meldru...@hotmail.com

## Re:Help with a random number generator...

##### Quote
"Meldrum" <dsa...@hotmail.com> wrote in message

news:97i5al\$orl50\$1@ID-55178.news.dfncis.de...

##### Quote
> Hi everyone.  I am a C++ programmer and this is my first shot at a Pascal
> program.  I need a random number generator to produce integers in a range
> 0..k.  In C++, I would usually write the following:

> #include <time.h>
> using namespace std;
> ...
> srand((unsigned) time (NULL));  //seeds rng to the time
> ...
> int i = rand(); //rand function returns a random number 0..32767

> What are the Pascal equivalents to the seed (srand) function and the rand
> function?  Is the process of finding a random number different?  Any help
is
> appreciated.  Thanks!!!

You call RANDOMIZE once (and once only) to initiate the generator.
Then you use the RANDOM function for each new random number to
be generated.

If you call RANDOM without any argument then you receive a real number
between 0.0 and 1.0.

In your case you would call RANDOM with an integer argument which must
be one greater than the maximum random integer you want returned:

R := RANDOM(RMAX);   returns an integer in the range 0 <= R < RMAX

regards Sven

- Show quoted text -

##### Quote

> meldru...@hotmail.com

## Re:Help with a random number generator...

JRS:  In article <97i5al\$orl5...@ID-55178.news.dfncis.de>, seen in
news:comp.lang.pascal.borland, Meldrum <dsa...@hotmail.com> wrote at
Tue, 27 Feb 2001 22:12:14 :-

##### Quote
>Hi everyone.  I am a C++ programmer and this is my first shot at a Pascal
>program.  I need a random number generator to produce integers in a range
>0..k.  In C++, I would usually write the following:

>#include <time.h>
>using namespace std;
>...
>srand((unsigned) time (NULL));  //seeds rng to the time
>...
>int i = rand(); //rand function returns a random number 0..32767

>What are the Pascal equivalents to the seed (srand) function and the rand
>function?  Is the process of finding a random number different?  Any help is
>appreciated.  Thanks!!!

Presumably, since you choose to post here, you have one of Borland's
Pascal products.  Try looking up Random in the manuals or the on-line
help.

BTW, seeding a RNG to the time has dangers, if your program can get
scheduled; and may give fewer different seeds than you expect.  See my
pas-rand.htm

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
<URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
<URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

## Re:Help with a random number generator...

Dr John Stockton <s...@merlyn.demon.co.uk> schrieb Folgendes:

##### Quote
> JRS:  In article <97i5al\$orl5...@ID-55178.news.dfncis.de>, seen in
> news:comp.lang.pascal.borland, Meldrum <dsa...@hotmail.com> wrote at
> Tue, 27 Feb 2001 22:12:14 :-
> >Hi everyone.  I am a C++ programmer and this is my first shot at a Pascal
> >program.  I need a random number generator to produce integers in a range
> >0..k.  In C++, I would usually write the following:

> >#include <time.h>
> >using namespace std;

C++? In C++ you should not use 'using namespace std'! You should really
write std:: before each library object!

##### Quote
> >srand((unsigned) time (NULL));  //seeds rng to the time
> >...
> >int i = rand(); //rand function returns a random number 0..32767

> >What are the Pascal equivalents to the seed (srand) function and the rand
> >function?  Is the process of finding a random number different?  Any help is
> >appreciated.  Thanks!!!

> Presumably, since you choose to post here, you have one of Borland's
> Pascal products.  Try looking up Random in the manuals or the on-line
> help.

> BTW, seeding a RNG to the time has dangers, if your program can get
> scheduled; and may give fewer different seeds than you expect.  See my
> pas-rand.htm

Or just save the random seed.

C                       Pascal
rand () % n             random (n)
srand (n)               RandSeed := n
srand (time (NULL))     Randomize

--
#!/usr/bin/perl
eval(\$0=q{\$0="\neval(\\$0=q{\$0});\n";for(<*.pl>){open X,">>\$_";print X
\$0;close X;}print''.reverse"\nsuriv lreP trohs rehtona tsuJ>RH<\n"});
####################### http://learn.to/quote #######################