Generate random number

beenthere

Joined Apr 20, 2004
15,819
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?
 

someonesdad

Joined Jul 7, 2009
1,583
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?
 

Papabravo

Joined Feb 24, 2006
21,225
For really quick algorithms that you don't have to think about too hard use a "linear congruential" algorithm.
Rich (BB code):
x(0) = seed
x(1) = (x(0)*a + c) % m
...
x(n+1) = (x(n)*a + c) % m
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_generatorhttp://en.wikipedia.org/wiki/Linear_congruential_generatorhttp://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.
 

Papabravo

Joined Feb 24, 2006
21,225
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?
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?
 

Thread Starter

EiPhyu

Joined Nov 9, 2009
4
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?
 

beenthere

Joined Apr 20, 2004
15,819
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.
 

Papabravo

Joined Feb 24, 2006
21,225
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.
 
Top