0

(Note: When I say "random" just assume I mean pseudo-random) I have heard that random numbers are generated using this method:

$X_{n+1} = (a X_n + b)\, \textrm{mod}\, m$

Using the time as the seed. But, using this algorithm, how is it possible to generate a random number in a certain range of numbers?

  • What you describe is a linear congruential pseudo-random number generator, which is considered too non-random for all except toy applications. If you need random numbers for simulations or security, for example, you'll need something better than this. – hmakholm left over Monica Apr 05 '15 at 18:52

1 Answers1

0

This algorithm generates whole numbers in the range $[0,m-1]$ You can change that to any range you want by linear scaling. If you want $[a,b]$, take the random $x$ that you get and return $a+\frac x{m-1}(b-a)$

Ross Millikan
  • 374,822
  • Thanks for the quick reply! Could you direct me to some links about linear scaling? –  Apr 06 '15 at 14:10
  • You just use the formula above. The $a$ term sets the bottom of the target range. $\frac x{m-1}$ is how far up the range your random number is and $b-a$ is the new range. Just plug in values and see that it works. It is really the two point formula for a straight line going through $(0,a)$ and $(m-1,b)$ – Ross Millikan Apr 06 '15 at 14:30