0

I'm using function random()%some integer in a method of one of my app's classes and I have no idea where to put srandom (time (NULL)) to generate not pseudorandom but true random numbers. I have already put it in viewDidLoad and viewWillAppear but it doesn't help.

- (NSMutableDictionary *)getUsersFromServer
{
      srand(time(NULL));
      //here we're getting list of users from the server
      NSMutableDictionary * users = [[[NSMutableDictionary alloc] init] autorelease];

     for (int i = 0;i < 19;i++)
     {
      int wins    = rand()%100; float f_wins = (float)wins;
       int losses  = rand()%100;     float f_losses = (float)losses;
      int withdr  = rand()%100;         float f_withdr = (float)withdr;
      float win_per = f_wins / ((f_wins + f_losses + f_withdr) / 100.0);

      [userresults setArray:[NSMutableArray arrayWithObjects:[NSNumber numberWithInt:wins],
                                                   [NSNumber numberWithInt:losses],
                                                                                [NSNumber numberWithInt:withdr],
                                                                                [    NSNumber numberWithFloat:win_per],
                                                                                 nil]]; 
          [users setObject:userresults forKey:[NSString stringWithFormat:@"Pfeffer ID %i",i]];
   }

    [userresults release];
     return users;
}

something like this... the code looks awful but its sense is understandable. rand() produces same numbers for each loop iteration. If I use arc4random() it changes nothing. still same numbers

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • 1
    Err, it is not making "pseudo random numbers true random numbers", `srandom`, `srand`, etc just set the seed, or initial state, [of a *pseudo random* number generator](http://en.wikipedia.org/wiki/Pseudorandom_number_generator). Generally the seed is set once (before `rand`/`random` are used), and preferably not based on the ticks directly... I am not sure what recommendations there are for IOS otherwise. –  Nov 11 '11 at 17:48
  • ok doesn't matter whether it is IOS or something else. where srand(time(NULL)) is supposed to be written to be seen by rand() in a class method? – Andrey Chernukha Nov 11 '11 at 17:59
  • 1
    FWIW, `arc4random` was recommended [in this post](http://stackoverflow.com/questions/5912799/srandtimenull-on-iphone-dont-work) relating to IOS. –  Nov 11 '11 at 17:59
  • Yes. Make sure `srand` is indeed called (first) and that it is not called repeatedly (which can throw off the internal state). Also, make sure to use `srand/rand` or `srandom/random` as a pair. –  Nov 11 '11 at 18:02
  • i know, i have already tried it, it generated same number too, unfortunately – Andrey Chernukha Nov 11 '11 at 18:03
  • Post code then. Because what you are seeing "shouldn't happen" from everything discussed above. Also take time to verify the seed is indeed different. –  Nov 11 '11 at 18:04
  • @Andrey Chernukha rand() works fine. What do you mean by "notthing works" ? .Anyhow, call srand() once , at program startup. Calling it at the start of a function is pretty pointless. – nos Nov 11 '11 at 18:17

1 Answers1

2

I don't think this is a issue about randomness or seeds at all, I don't use Objective-C, but ...

// for each loop iteration:
  [userresults setArray: .... ]      // <-- modify object known as userresults?
  [users setObject:userresults ....] // <-- isn't that the SAME userresults object?

That is, I believe you have the same object multiple times in users: shouldn't a new result object be created each iteration?

Also, see srand() — why call it only once? -- for why using srand at the top of this method might not be ideal. Alternatively, this post recommends arc4random as it does not require a manual seeding and is a "stronger" pseudo-random generator.

Happy coding.

Community
  • 1
  • 1