3

In my current project I need multiple random number generators because I need to be able to repeat their sequences independently from each other. So far I did not find any way to achieve this with the standard objective-c random number generators, because they only have one global state.

I think having an random number generator class would solve my problem. I could create several instances which I could reset individually.

Is something like this already available? I was not able to find any random number generator implementation in objective c. I would like to avoid implementing it myself because I have no experience with random numbers and I think it is something that's hard to get right.

sietschie
  • 7,425
  • 3
  • 33
  • 54

2 Answers2

6

I have a random class, based on the Mersenne Twister algorithm, which you can get from my dropbox here.

It's rather old, and isn't compiled for ARC, but that doesn't make it any less good :)

Example code:

MTRandom *randWithSeed = [[MTRandom alloc] initWithSeed:12345];
double d = [rand nextDouble];
int i = [rand nextInt];

MTRandom *timeBasedRand = [MTRandom new]; // seeds with current time
double d2 = [timeBasedRand nextDouble];
int i2 = [timeBasedRand nextInt];

EDIT: If you want to be really cool, you can use this:

enter image description here Source

James Webster
  • 31,873
  • 11
  • 70
  • 114
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • This looks exactly like what i want. How about the license? Am I allowed to do anything I want with your code? – sietschie Jun 16 '12 at 14:58
  • @sietschie don't worry about it. No license, but I wouldn't mind a link when the game is out :) – Richard J. Ross III Jun 16 '12 at 14:59
  • If you don't like MT, then there are plenty of other possible PRNGs associated with names like George Marsaglia or Pierre L'Ecuyer. – rossum Jun 16 '12 at 15:49
  • 1
    +1 for great answer; I wish I could make it +2 for the XKCD reference – dgund Jun 16 '12 at 16:37
  • Mersenne twister is a bit old I would prefer using SFMT: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html it is faster, smaller state and gives 'better' results. – Charles Beattie Jun 18 '12 at 09:28
1

Have you tried

srandom(seed);

and then calling

random();

? If the seeds are the same then you should get the same sequence of random numbers.

futureelite7
  • 11,462
  • 10
  • 53
  • 87
  • the only issue with that is that the OP stated he wanted to have multiple randoms at the same time. – Richard J. Ross III Jun 16 '12 at 15:01
  • Yes. But I need several random sequences, which I can reset independently. – sietschie Jun 16 '12 at 15:02
  • Can you pre-generate the random sequences? Since the numbers are fixed you can probably just generate arrays of 'random' numbers and store them. Do you expect to change the random sequences all the time? – futureelite7 Jun 16 '12 at 15:05
  • That would be possible. But it seemed easier to just save the seed instead of saving the whole sequence. Plus all the bookkeeping that probably would be needed to make it work. A RNG-Object looked like a much cleaner solution to me. – sietschie Jun 16 '12 at 15:19