This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Rand function Method

I would like to know on which method Keil rand() function generates the random values.Are they using LCM or Linear Feedback shift register or any other method?

Please help me in this.

Thanks
Gopi

Parents
  • There is a special check for the LFSR being in the all-ones state, although I am not clear why that is required.

    Hm. Isn't that to avoid the LFSR getting trapped into a degenerate state where it will keep producing the same output forever? LFSRs work fine until they happen to generate a random all-1s, and then they're stuck producing that all-1s forever?

    I thought C libraries were usually linear congruential. Interesting.

    Pseudo-random number generators (RNG) in software typically are designed to allow for a "seed" value which determines the sequence of random numbers that are generated. This is useful for many tasks (testing, simulation) where you want to be able to test random input, but you also need to repeat tests with the exact same "random" input. You set the seed value for the RNG with the srand() function. If you don't call srand(), then you'll have the default seed value (1) every time you start your program, and thus have the same series of random numbers, without having to store them all.

    To generate "really" random numbers, you also have to randomize the seed. Typical methods for doing so are to use low-order bits of a fast clock as the seed. Other methods are to use the time(s) of user key input or network packet arrival as a source of randomness for the seed value. Thus, you can get repeatable or non-repeatable sequences of random numbers, depending on whether you know what your seed value is.

    If you're really dead serious about the randomness of your RNG (say, you're building a slot machine), you won't use a software function at all, but will design some dedicated hardware that generates truly random numbers, say by amplifying noise. Any software function is going to be ultimately deterministic, even if it's really complicated; the only truly random number generators we know of are quantum events.

    Read all about pseudo-random number generators:
    http://www.wikipedia.org/wiki/Pseudo-random_number_generator

Reply
  • There is a special check for the LFSR being in the all-ones state, although I am not clear why that is required.

    Hm. Isn't that to avoid the LFSR getting trapped into a degenerate state where it will keep producing the same output forever? LFSRs work fine until they happen to generate a random all-1s, and then they're stuck producing that all-1s forever?

    I thought C libraries were usually linear congruential. Interesting.

    Pseudo-random number generators (RNG) in software typically are designed to allow for a "seed" value which determines the sequence of random numbers that are generated. This is useful for many tasks (testing, simulation) where you want to be able to test random input, but you also need to repeat tests with the exact same "random" input. You set the seed value for the RNG with the srand() function. If you don't call srand(), then you'll have the default seed value (1) every time you start your program, and thus have the same series of random numbers, without having to store them all.

    To generate "really" random numbers, you also have to randomize the seed. Typical methods for doing so are to use low-order bits of a fast clock as the seed. Other methods are to use the time(s) of user key input or network packet arrival as a source of randomness for the seed value. Thus, you can get repeatable or non-repeatable sequences of random numbers, depending on whether you know what your seed value is.

    If you're really dead serious about the randomness of your RNG (say, you're building a slot machine), you won't use a software function at all, but will design some dedicated hardware that generates truly random numbers, say by amplifying noise. Any software function is going to be ultimately deterministic, even if it's really complicated; the only truly random number generators we know of are quantum events.

    Read all about pseudo-random number generators:
    http://www.wikipedia.org/wiki/Pseudo-random_number_generator

Children
  • If you're really dead serious about the randomness of your RNG (say, you're building a slot machine), you won't use a software function at all, but will design some dedicated hardware that generates truly random numbers, say by amplifying noise
    If a button press or other human interaction tahes place for each need of a random number, the read of a free running timer will do just fine for true randomness.

    Erik

  • The sequence of numbers produced by an algorithm (especially the common ones like LCG and LFSR) have some statistical correlation. They all involve state that ties them to previous results.

    The button press / timer is a reasonable way to get a seed for most purposes. But, as I said, if you're really concerned about producing an uncorrelated stream of numbers, you'll need some hardware support. There are relatively few applications that need to obsess over the randomness of their numbers in this detail, though. Do you really need to go to this much trouble (http://www.fourmilab.ch/hotbits/)? Well, maybe if you're doing cryptography, or generating lotto numbers, or doing some heavy duty Monte Carlo simulation.

    A nice summary of the topic:
    http://www.faqs.org/rfcs/rfc1750.html