iPhone: random() function gives me the same random number everytime

前端 未结 8 2122
情书的邮戳
情书的邮戳 2020-12-09 14:02

I am using function random()%x for the generation of a random number, but every time I start the application I see that it creates or generates the same number.

Lik

相关标签:
8条回答
  • 2020-12-09 14:07

    Obligatory XKCD comic:

    alt text

    0 讨论(0)
  • 2020-12-09 14:12

    Don't forget you need to seed the random number generator via srandom before using it, with a changing value such as the current time.

    0 讨论(0)
  • 2020-12-09 14:14

    For newbies that come across this post:

    The random() function produces a pseudo-random sequence. random() ways gives you the same pseudo-random sequence each time you use it. You need to "seed" the sequence to pick a different starting point so each time you run it so it appears different. You can use the system time to seed (srandom(time(NULL)) or use the helper function srandomdev().

    To experiment try:

    #include "stdio.h"
    
    int main(void) {
        int i;
        for (i = 0; i < 10; i++)
            printf("%d\n", random());
    
        return 0;
    }
    

    You'll always get the same sequence, on my computer it gives:

    1804289383
    846930886
    1681692777
    1714636915
    1957747793
    424238335
    719885386
    1649760492
    596516649
    1189641421
    

    More reading:

    • The random man page for more information. (Run man random from Terminal.)
    0 讨论(0)
  • 2020-12-09 14:17

    In your application delegate:

    - (void) applicationDidFinishLaunching:(UIApplication *)application 
    {
        srandom(time(NULL));
    
        // ...
    
        for (int i = 0; i < 100; i++) {
          NSLog(@"%d", random());
        }
    }
    

    The reason this works is because pseudorandom number generators require a starting, or seed value. By using the time, you are more likely to get different sequences of "random" numbers upon each execution.

    If you do not specify a seed value, the same seed is used on each execution, which yields the same sequence. This is usually undesired behavior, but in some cases it is useful to be able to generate the same sequence, for example, for testing algorithms.

    In most cases, you will want to specify a seed value that will change between runs, which is where the current time comes in handy.

    0 讨论(0)
  • 2020-12-09 14:18

    Do use srandom (or the equivalent for your random number function of choice), but also use conditionals around it, so that if you are debugging, things always happen the same way. I also tend to put NSLog warnings when doing things like that, so I don't ship brian-dead code.

    #if DEBUG==0
    srandom(time(NULL));
    #else
    NSLog(@"Debug build: Random numbers are not random!");
    #endif
    

    or

    if(!debuggingBuild)
        srandom(time(NULL));
    else
        NSLog(@"Debug build: Random numbers are not random!");
    
    0 讨论(0)
  • 2020-12-09 14:20

    arc4random will be better solution than rand() or random(). See this.

    0 讨论(0)
提交回复
热议问题