IPhone function: random () gives me the same random number every time

I use the random ()% x function to generate a random number, but every time I run the application, I see that it creates or generates the same number.

How I put some images randomly based on this random number, and I see that all the images are placed in the same place no matter how many times I run the application.

+6
random iphone
source share
8 answers

You are most likely lucky with arc4random() , you do not need its seed explicitly, and this seems to be the β€œbest” random.

+16
source share

Mandatory XKCD comic :

alt text

+15
source share

In the application application:

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

The reason for this is because pseudo-random number generators require an initial or initial value. Using time, you are more likely to get different sequences of "random" numbers at each execution.

If you do not specify an initial value, the same seed is used for each execution, which gives the same sequence. This is usually undesirable 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 need to specify an initial value that will vary between runs, where the current time comes in handy.

+14
source share

For newbies who fall into this post:

The random() function creates a pseudo-random sequence . random() methods give you the same pseudo-random sequence every time you use it. You need to "minify" the sequence to select a different starting point, so each time you run it, it looks different. You can use the system time for the seed (srandom(time(NULL)) or use the srandomdev() helper function.

To try:

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

You will always receive the same sequence, on my computer it gives:

 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421 

Additional Information:

  • Random user page for more information. (Run man random from the terminal.)
+4
source share

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

+4
source share

Do not forget that you need to sow the random number generator through srandom before using it, with a changing value, such as the current time.

+2
source share

First call srandomdev ().

srandomdev ();
long my_rand = random ();

+2
source share

Use srandom (or the equivalent for the selected random number function), but also use the conventions around it, so if you are debugging, everything always happens the same way. I also tend to set NSLog warnings when doing this, so I do not send 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
source share

All Articles