Sebastian Nozzi

Posted on

Random numbers

In this blogpost we are going to take a look on how to produce random numbers within a certain range in MiniScript / Mini Micro.

The `rnd` function

In MiniScript (and by extension Mini Micro) the way to get random numbers is by using the `rnd` intrinsic function.

Calling this function gives you a floating-point random number between 0 and 1.

``````> print rnd
0.147535
> print rnd
0.421682
``````

Bigger range

If we want a bigger range of random numbers all we have to do is multiply the result of `rnd` by some other number. For example: if we want random numbers between 0 and 20 we evaluate `rnd * 20`:

``````> print rnd * 20
15.439491
> print rnd * 20
19.455438
> print rnd * 20
8.460014
``````

The `floor` function

Often we need random integers (whole numbers). One way to convert a floating-point number to an integer is by calling another intrinsic function: `floor`. Applying this function to a floating-point number will effectively strip it from the decimal digits, leaving only the "integer" part.

``````> print floor(0.14)
0
> print floor(42.9)
42
``````

Situation so far

Combining these three things:

1. ... calling `rnd` for random numbers between 0 and 1
2. ... multiplying `rnd` by a factor
3. ... applying `floor` to the result

... will produce random integers between 0 and the desired factor.

For example:

``````> print floor(rnd * 20)
13
> print floor(rnd * 20)
6
``````

Numbers in any range

What if we wanted to have random numbers for a certain range starting above 0?

what we need to do is to sum some fixed number to the result. When doing so we are "shifting" the range of results, so we need to be careful about the end of the range, which is also shifted. We usually need to compensate for that by subtracting from the factor, which corrects the range-size.

For example, let's say we want random numbers between 100 and 300.

What is the solution?

Maybe `floor(rnd * 300)`? No, this would give us numbers from 0 to 300.

Maybe `100 + floor(rnd * 300)`? No. Because `floor(rnd * 300)` gives us numbers from 0 to 300, adding 100 to it would give us numbers from 100 to 400 (not what we want).Remember: the end-range is also shifted.

Because the end-range is shifted, we need to compensate by subtracting from the factor (to correct the range size).

The correct answer is general terms:

``````RANGE_SIZE = END_RANGE - START_RANGE
START_RANGE + floor(rnd * RANGE_SIZE)
``````

And in our case:

``````100 + floor(rnd * (300-100))
``````

Or, simplified:

``````100 + floor(rnd * (200))
``````

Some examples:

``````> print 100 + floor(rnd * 200)
282
> print 100 + floor(rnd * 200)
102
> print 100 + floor(rnd * 200)
183
``````

Summary

With this we learnt how to generate random whole-numbers (integers) in a range between two arbitrary numbers in Mini Script.

In a future posts we will put this knowledge to use and have some fun with it.

Happy coding!

JoeStrout

Great stuff — looking forward to the follow-up posts!