I made a local value named "s", it uses math.random() with decimals, but it keeps being returned as an integer (Without decimals..), i'm using this to make meshes at random scales.
1 | local s = math.random( 6.4 , 9.6 ) |
2 | Mesh.Scale = Vector 3. new(s,s,s) |
3 | print (s) |
4 | --6 |
5 | --8 |
6 | --7 |
What's happening?
math.random(a,b)
only returns integers between a
and b
.
This is usually desirable, so that, for example, picking a random element from a list is possible:
math.random(1,#tab)
.
Perhaps you say -- "But the paramaters aren't integers! It should know that's not what I want!" However, that would result in inconsistency - multiply the range by ten and suddenly it no longer densely fills the space.
math.random()
(no parameters) generates uniform numbers in the interval [0, 1).
Use it to generate arbitrary numbers:
1 | local s = 6.4 + ( 9.6 - 6.4 ) * math.random() |
Check out the wiki page on random. Giving math.random() two parameters as the top and bottom limit always returns integers, or whole numbers. Instead, I see two options you may be able to consider.
First, consider that math.random(), without parameters, returns a random decimal between 0 and 1. You can use this and scale that range to your needs. 9.6 - 6.4 = 3.2 Your range is 3.2, and your bottom value is 6.4. So math.random() * 3.2 + 6.4 would do the trick.
Alternatively, you can multiply your range by something in the paremeters, and then divide it by the same value after you get the value back. I tend to do this. For example, math.random(640, 960) / 100.
Hope this helps!