Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How does math.random work?

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

✓ Accepted answer

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)

2 comments

0

Thanks!

0

No problem!

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

1 comment

2

Didn't see Nifer's post, sorry. :)

This discussion is preserved as it was. New answers, comments, and votes are closed.