3

I am using random number generation as part of a procedure for minimising a function (using the Nelder-Mead simplex algorithm) in objective-c (for iOS). I have used arc4random() because it seems to be recommended everywhere on the grounds that a) it doesn't need to be seeded and b) it gives higher-quality random numbers than alternatives such as rand() and random(). I generate doubles between 0 and 1 using

#define ARC4RANDOM_MAX      0x100000000
-(double) Rnd{
return (double)arc4random() / (double)ARC4RANDOM_MAX ; }

However, to test the procedure I need to generate repeatable sequences of random numbers, and I can't find any reference to a way to initialise arc4random() to do this. Is it the case that arc4random() cannot be initialised to give a repeatable sequence? If so, how can anyone implement an automated unit test when every test will result in a different answer? Do I need to use the "lower quality" random numbers from random()? Thanks for your help.

Dhara
  • 4,093
  • 2
  • 36
  • 69
Rob Bullen
  • 75
  • 4

1 Answers1

1

The arc4random function gets random numbers from a pool over which it has no control. It has no mechanism to provide repeatability. For unit tests, you'll have to use something else.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278