delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/11/03/16:48:58

From: vcarlos35 AT juno DOT com
To: djgpp AT delorie DOT com
Date: Tue, 3 Nov 1998 16:44:29 EST
Subject: Re: Random Number
Message-ID: <19981103.164437.5863.0.vcarlos35@juno.com>
References: <71kbql$a5$1 AT zoltar DOT gatchaman DOT fr>
X-Mailer: Juno 1.49
X-Juno-Line-Breaks: 0,2-43
Reply-To: djgpp AT delorie DOT com

On 2 Nov 1998 13:24:37 GMT root AT zoltar DOT gatchaman DOT fr (Pierre MAZIERE)
writes:
>Hi everybody,
>I got a problem with the Random Number Generator: I use it this way 
>
>int i;
>
>srandom(int seed)  /* what is the meaning of this 'seed' ?*/

The seed initializes the PRNG. Try

int x;

printf("Enter a seed: ");
scanf("%d",&x);
srandom(x);

or use one of the clock/time functions.

>for( ... )
>{
> i=random();
> printf("%u \n",i);
> ...
>
>};
>
>Then  things:
>- Every time I launch the executable, the sequence of so called random
>  numbers printed on screen is always the same ( so where is the 
>randomized
>  stuff ?)
>- How can I manage to have 'i' between 0 and MaxRandomNumber ?

The easiest way would be i = random() % (MaxRandomNumber+1),
but the probability distribution of i will not be uniform on
(0,MaxRandomNumber) unless:
-PRNG is itself uniform on (0,MAXINT) 
-MAXINT % (MaxRandomNumber+1) == 0
But the skew will be probably be minimal, unless you're doing
extensive Monte Carlo analysis.

BTW, what is the maximum period of the library random() function?

___________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com/getjuno.html
or call Juno at (800) 654-JUNO [654-5866]

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019