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

How does math.random work?

Asked by
iLegitus 130
10 years ago

So math.random is soppost to give you a random number. But i am trying to actually control that number. ( Control as in give it A Max and Minimum ) How would i so?

2 answers

Log in to vote
4
Answered by
Nifer2 174
10 years ago

You would use it like this...

local number = math.random(min, max) -- Just replace the min and max with numbers of your choice. Example:

number = math.random(1, 10)
print(number)
0
Thanks! iLegitus 130 — 10y
0
No problem! Nifer2 174 — 10y
Ad
Log in to vote
9
Answered by 10 years ago

math.random is a mathematical function that gives you a random number. There are two arguments in this function, m and n.

If you give no arguments and just do this in the command bar in Studio:

print(math.random())

It returns and prints a number between 0 and 1 in the console.

If you give one argument, it returns a number between 1 and your argument (m). For example:

print(math.random(8)

It returns and prints a number between 1 and 8 in the console.

If you give two arguments, it returns a number between argument 1 (m) and argument 2 (n). For example:

print(math.random(3,5)

It returns and prints a number between 3 and 5 in the console.

Now if we use this frequently, you'll start to realise the numbers aren't usually "random" each time you start up a new game. We can use math.randomseed to make the numbers returned more random and different each time. For example, we'll put this script into Workspace and run a test server to test the script.

math.randomseed(tick()) --Seeds the random number function by your local time.

print(math.random(1,10))

If you run one server, it'll print a random number between 1 and 10. If you ran another server without the random seed function, you'll probably get the same number as last time. If you run another server now, you'll get a different number from last time.

Sorry if I went to in depth. If you need a more simpler explanation, check the wiki here: http://wiki.roblox.com/index.php?title=Function_dump/Mathematical_functions#math.random

2
Didn't see Nifer's post, sorry. :) Spongocardo 1991 — 10y

Answer this question