Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

math.random() Keeps returning integers?

Asked by
Nymint 85
9 years ago

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.

local s=math.random(6.4,9.6)
Mesh.Scale=Vector3.new(s,s,s)
print(s)
--6
--8
--7

What's happening?

2 answers

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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:

local s = 6.4 + (9.6 - 6.4) * math.random()
0
Thank you guys and mostly BlueTaslem, as always. (: Nymint 85 — 9y
Ad
Log in to vote
1
Answered by
Defaultio 160
9 years ago

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!

Answer this question