Skip navigation.
 
mlRe: Random Number Woes
FROM : James Robinson
DATE : Fri Jan 10 11:23:02 2003

On Fri, 10 Jan 2003, iUNI Technologies Corp. wrote:

> srandom(45);


  This seeds the pseudo-random number generator with the same number every
time, resulting in the same pseudo-random sequence every time.  This is a
feature (think debugging).

> float randValue  = ((float) random() / (float) RAND_MAX);


  Cast to double.

> NSNumber *myRand = [[NSNumber alloc] initWithInt:((randValue)+40)];
> [sdata appendString:[myRand stringValue]];
> [myRand release];


  Use:

  srandom(time(NULL));

  time(NULL) returns the current system time in seconds, which is unlikely
to be the same twice.

  Also, the standard C calls are srand() and rand() (in <stdlib.h>).
srandom() and random() are UNIX-specific, but they can be higher quality.
Which you use depends on how important portability is to you.

  Hope this helps,

--
James

Related mailsAuthorDate
mlRandom Number Woes iUNI Technologies… Jan 10, 11:06
mlRe: Random Number Woes James Robinson Jan 10, 11:23
mlRe: Random Number Woes Jeff Szuhay Jan 10, 11:31
mlRe: Random Number Woes Mike Vannorsdel Jan 10, 11:35
mlRe: Random Number Woes Timothy J. Wood Jan 10, 12:42
mlRe: Random Number Woes iUNI Technologies… Jan 10, 12:51
mlRe: Random Number Woes James Robinson Jan 10, 12:58
mlRe: Random Number Woes Steve Quirk Jan 10, 13:02