Picking a range of numbers with math.random()

math.random() seems to only pick through the two numbers provided, is there any way to pick through a range of numbers starting through the first one provided and ending at the second one provided?

I’ve tried providing multiple math.random() statements which doesn’t seem to work and even multiple arguments: math.random(1, 2, 3) … which also doesn’t work

Is there a certain function for this? The closest thing I can think of is math.randomseed() … but that doesn’t seem to do what I want it to do.

just call math.random multiple times in variables – could work

What behavior are you trying to achieve? Do you have any examples?

You can create an array with the numbers you want and randomly pick through that:

local randArray = {1, 3, 5, 9, 13}

--> picks a random number in [randArray]
local randomSelection = randArray[math.random(1, #randArray)]
2 Likes

Setting a sounds PlaybackSpeed to a random value between a minimum number and a maximum number

This should work perfectly. I’ll test it and see if it works.

Could you not just do

math.random(min, max)

You said in your post:

math.random() seems to only pick through the two numbers provided

But this isnt true. Calling math.random(1, 10) does not pick either 1 or 10, it picks any integer between 1 and 10, which means it could include 1, 2, 3, 4, 5, etc…
If you need a “random value between a minimum number and maximum number” then this is exactly what math.random does.

Just wanted to clear that up.

I see. It’s odd considering most of the times I used math.random() and printed the number it picks it only prints one of the two numbers

Repeating numbers are a tell-tale sign of non-uniformity. Humans erroneously believe that a distribution like; 1, 5, 7, 3, 2, 8, 9, 4, 6 is random whereas machines correctly recognise that a distribution like; 4, 4, 1, 4, 1, 1, 2, 7, 2 is random.

2 Likes