Random numbers: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Emess
major edits, especially with awesome tick()
>Emess
major edits, especially with awesome tick()
(No difference)

Revision as of 17:42, 23 December 2010

Introduction

There is a random number generator in Roblox that seems to generate random numbers. But do a test, and insert this script into your place:

for i = 1, 10 do
print(math.random(1,100))
wait(1)
end

Test the place, and look at the output window. Write down the numbers you get, and exit the test. Then do the test again, and again write down the numbers you get. Notice something weird? Those 10 very random numbers are similar everytime you run a test (or play online for that matter).

This article explains how to achieve truly random numbers that are NEVER the same for every time a place is played

The General idea

Everytime a script in a place does math.random(), the random generator generates a random number. If this is done at exactly the same time, with exactly the same random numbers everytime, then the random number sequence will be the same every time (like the test at the top of this page).
However, if you were able to make the place generate random numbers at random times (in addition to your normal random generations), then the sequence of random numbers would change, right? Yes!
The problem is, if the randomly generated randomizing of the random number generator is the same everytime it randomizes something, then the sequence will still be the same every time!

How to generate TRULY random numbers

You need to make a script do math.random(x,y) at at least one TRULY random time, and then the rest of the randomized numbers will be truly random. Scripting is, of course, needed to make this happen. Some truly random events can be things like:

  • Number of players in your place
  • Players movement
  • Other real-life influences on the game

There are 3 things this can influence that in general will make stuff very random: math.random(x,y)

  • If you change either x or y (random numbers min and max)
  • If you change when the math.random happens
  • If you change how often the math.random happens

If you can find a way to make something truly random influence any of the three listed items, then you will be able to generate truly random numbers!

math.randomseed()

The best way to fix this problem is a function called math.randomseed(). What the function does is set a seed for the random number generator.

The problem is finding a good seed. Fortunately, Roblox made the function tick(). What that does is print the amount of seconds that elapsed since UNIX time (1/1/1970 0:00:00).

To integrate it, simply put tick() inside the math.randomseed() parenthesis. Something like this:

math.randomseed(tick())

Using the Seed

You could put math.randomseed() anywhere. It is recommended to put it at the beginning of the script (it doesn't really matter, as long as if you put it before you use math.randomseed()). Now, let's use the experiment script that we used before and use math.randomseed with it!

math.randomseed(tick())

for i = 1, 10 do
print(math.random(1,100))
wait(1)
end

Congratulations! Your random numbers will now be much more random than before!