Random numbers within a range?

131 Views Asked by At

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

1

There are 1 best solutions below

2
On BEST ANSWER

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