Generate random number

Discussion in 'Programmer's Corner' started by EiPhyu, Nov 12, 2009.

  1. EiPhyu

    Thread Starter New Member

    Nov 9, 2009
    4
    0
    I'm writing a program to generate random number in keil c. Anyone,can help me..
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    By now, there must be a large number of such routines. Have you tried one, and is your request because the result is not very random? How are you seeding the RNG routine?
     
  3. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    You didn't ask a question, so it's hard to know how to help you. Please read the forum guidelines on how to post good questions to improve your chances of getting good help.

    There is a huge amount of literature on the topic of random number generation and it has been well-studied over the last half century. As always, start with Knuth. If you're just after a plug and chug algorithm, consult the Numerical Recipes books. Is there any reason you can't use the routines from your standard library?
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,145
    1,791
    For really quick algorithms that you don't have to think about too hard use a "linear congruential" algorithm.
    Code ( (Unknown Language)):
    1.  
    2. x(0) = seed
    3. x(1) = (x(0)*a + c) % m
    4. ...
    5. x(n+1) = (x(n)*a + c) % m
    6.  
    The period of the LCG is at most m, and it will have a full period iff
    1. c and m are relatively prime
    2. a-1 is divisible by all prime factors of m
    3. a-1 is a multiple of 4, if m is a multiple of 4
    See the following:
    http://en.wikipedia.org/wiki/Linear_congruential_generator
    http://www.cs.princeton.edu/courses/archive/fall02/cs126/assignments/cycle.html

    The sequences that are produced do have limitations as the wiki makes clear. The Mersene twister is supposed to have better randomness properties than most of the LCG algorithms.
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,145
    1,791
    Actually it is a question if we are willing to relax the rules on word order, punctuation and dropped words for non-English speakers just a bit.

    Anyone,can help me.. --> Can anyone help me? OR Anyone, can you help me?


    Not too much of a strech if you ask me. A little flexibility goes a long way. The OP should get some credit for trying to follow the rules, and what does it hurt to be a bit flexible?
     
  6. EiPhyu

    Thread Starter New Member

    Nov 9, 2009
    4
    0
    The problem is that i can produce random numbers but the sequence of the random numbers are the same whenever i reset .How can I solve that problem?
     
  7. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    As the computational path is the same, about the only way to produce a different series of pseudorandom numbers is by varying the seed value.
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,145
    1,791
    The ability to reproduce a sequence is often an important feature of the pseudo random number generators.

    In any case, sampling an external phenomena is the only way to obtain different seeds after a RESET. This could be keyboard input, or some analog value, or β-decay from a radioactive source. How hard do you want to work at this for your purposes?

    Keep in mind, there are no algorithms that can produce truly random numbers. Pseudo-random is as good as it gets.
     
Loading...