Random Isn't That Random

Discussion in 'Mac Programming' started by Halfmaster1, Dec 29, 2010.

  1. Halfmaster1, Dec 29, 2010
    Last edited by a moderator: Dec 29, 2010

    Halfmaster1 macrumors newbie

    Dec 28, 2010
    I seem to be running into problem. I'm using a sort of random number generator (with of course, add stuff). I've found that every time I run my program, all ten of the numbers generated are the same. If I run the program again, the are ten different numbers, but still the same. (eg. the first time: 120 120 etc. second time, 653 653 653 etc..

    here's the code I used to test this random feature:
    while (loopCounter<maxAmount)
    	number = randomGenerator(roof);
    	loopCounter += 1;
    	printf("%d has been generated.\n", number);
    int randomGenerator(int max)
    	srand ( time(NULL) );
    	return (rand() % max + 2);
    roof is 999, and maxAmount is 10. randomGenerator should return a number from 2 to 1000 (which it does).

    I got the randomGenerator of the internet.

    Any help?
  2. chown33 macrumors 604

    Aug 9, 2009
    Where did you find this code? Be specific, because it's completely wrong, and I'd like to know where to avoid looking for examples.

    srand() is not intended to be called every time. It's only intended to be called once (or infrequently). Every time it's called, the generator is reseeded (i.e. reset with a seed). Since you reset the generator every time, all it can do is return the same value.

    Also, srand() and rand() are a bad random number generator. You can tell by reading the man page, where it says "bad random number generator" at the top of the page.


    Man pages can also be read using Xcode: Help > Open man Page...
    then type in srand and hit return.
  3. ulbador macrumors 68000


    Feb 11, 2010
  4. PatrickCocoa macrumors 6502a

    Dec 2, 2008

    1. Use the
     tag for any code you insert (see the "#" at the top of the box when you're creating a post).
    2. DEBUGGING: It sounds like the seed of the random number generator is set to the same number each time through the loop, but to a different number each time you run it. So I'd check out srand(time(NULL));, or more likely time(NULL). Is there some way you can print out time(NULL)? Research srand() to find out what it's doing. Research time() to find out what it's doing.
    3. There have been entire books written about generating random numbers, it turns out to be a fairly complex topic. 
    4. Try moving srand() to your calling routine or getting rid of it altogether.
    5. Find a different set of random number generating code in objective-C and see if it's different that the code you're starting with.
  5. gnasher729 macrumors P6


    Nov 25, 2005
    I'd also like to know that, especially since we had _exactly_ the same mistake a few weeks ago.
  6. Halfmaster1 thread starter macrumors newbie

    Dec 28, 2010
    You guys were right, I needed to move it.

    Oh, and for this program, it doesn't have to be very random, just without a noticeable pattern.

    Thankyou, when I need real randomness, I'll look into random instead of rand.

    Oh and I got rand from google. I knew rand gave random numbers, but not how to use it, so I googled rand.
  7. uwbadger macrumors member

    May 20, 2009
    Let me see if I can explain this a little more. "Random" numbers generated by software aren't actually random. What you're using is called a pseudo-random number generator (PRNG). Basically, a PRNG generates a very long sequence of numbers with statistical properties that simulate true random numbers. Given one number in this sequence, it should be nearly impossible to predict the next number. However, the process is still entirely deterministic: if you know the internal state of the PRNG, you can predict the next number. Because of this, you should give the PRNG an unpredictable starting point, a process called "seeding" the PRNG. This is what your call to srand is supposed to do: it gives the PRNG an initial state, after which it begins to generate numbers.

    In the code you posted, you seeded the PRNG with the current time in seconds. This isn't random or even very unpredictable, but it's often good enough. The problem is that you re-seeded the PRNG before generating each random number. This almost defeats the purpose of a PRNG, because you're no longer relying on the unpredictability and the (hopefully!) nice statistical properties of the PRNG. Instead, each number has a correlation with the time at which it's generated, and so predicting the next "random" number becomes much, much easier. In this case, it's even worse than that. Since your program runs in a fraction of a second, each call to time() returns the exact same value, and you keep resetting the PRNG to the exact same state. Since the PRNG is deterministic, this is why you get the same number over and over.
  8. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    But where did you get that code snippet? Remember, please be specific. We really would like to know where this bad information is originating.
  9. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    It is possible the OP concocted this from a source that showed how to get ONE pseudo-random number, and they took too much and put it into their own function.

  10. PatrickCocoa macrumors 6502a

    Dec 2, 2008
    Further Discussion

    The OP has his problem fixed, but to continue this discussion, there are times when you shouldn't seed your PRNG with a different value. For example, if you're debugging some code, you may want to have the same sequence of PRNs on every run so that you can tell if other (non-PRNG related) changes in your code are changing the output.

    Also off topic: how does uwbadger feel about Nebraska coming to the Big 10?

Share This Page