Seeding the random number generator with a variable

Discussion in 'Mac Programming' started by MorphingDragon, Jul 18, 2010.

  1. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #1
    How would you seed the random generator with a variable? All the examples on C I can find are using srandom(time(NULL)).

    I had an idea for generating random numbers on an iPhone that I want to play around with, using the GPS Data, Time, and Accelerometer/Gyro Data. Because that would be very hard to get the same seed number twice.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Just use arc4random() and let it seed itself. Don't try to out-clever it.
     
  3. MorphingDragon thread starter macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #3
    This isn't for a program, this is for a CompSci essay.
    Please just answer the question.
     
  4. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #5
    Wow, that was rude. If you don't provide all the information, don't expect people to assume that you know more than they do. Doing so on public forums is rude, and counter-productive to everyone's interest, including your own.
     
  5. MorphingDragon thread starter macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #6
    You'll have to excuse people, sometimes their squishy brains just want an answer.

    ]
    Thanks.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Then I would use srandomdev(), and not srandom().

    And if I were somehow compelled beyond reason to use srandom(), then I would seed it with data read from arc4random().

    I would then not call random() at all, because you didn't give that as a requirement. Instead, I would make the required seeding of srandom(), then call arc4random() to actually generate random numbers, because random() is weak and arc4random() is not.
     
  7. MorphingDragon thread starter macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #8
    I know that arc4random() has a better pseudonumber algorithm. But I didn't know about srandomdev()

    The essay isn't about generating numbers, its about coming up with a way to solve the random number problem without resorting to things like radiation. (And suitably backed up with things like physics/mathematics/CompSci theory and actual code snippets)

    ---

    I'm sorry if I'm a bit snippy.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Step 1: Read the fine man page.

    A few physical sources of entropy available to a typical computer:

    Shot noise (google it).

    Air turbulence in hard drives affects seek time.

    Frequency jitter in oscillators.

    Differences in arrival times of human-generated events (keystrokes, mousing, touches, etc.)

    If all you can come up with is radiation, you're not trying very hard.

    Be sure to look up shielding, too, because some sources can be biased by injecting RF externally, or even remotely.

    As to combining the entropy for high-quality random numbers, see the Yarrow algorithm. If you don't know how it was developed, see Schneier's "Applied Cryptography" for the chapter on PRNGs. And google NIST hash competition.
     

Share This Page