0
$\begingroup$

(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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Henning Makholm Apr 5 '15 at 18:52
0
$\begingroup$

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)$

$\endgroup$
  • $\begingroup$ Thanks for the quick reply! Could you direct me to some links about linear scaling? $\endgroup$ – user169330 Apr 6 '15 at 14:10
  • $\begingroup$ 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)$ $\endgroup$ – Ross Millikan Apr 6 '15 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy