Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arc4random initialisation

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.

like image 501
Rob Bullen Avatar asked May 30 '26 05:05

Rob Bullen


1 Answers

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.

like image 93
David Schwartz Avatar answered Jun 02 '26 11:06

David Schwartz