|
|
Line 1: |
Line 1: |
| {{CatUp|Tutorials}} | | {{CatUp|Tutorials}} |
| == Not really random == | | __TOC__ |
| The random number generator in roblox does seem to generate random numbers. | | |
| But do a test, and insert this script into your place:<br/>
| | == Random number generator == |
| | |
| | The random number generator in Roblox does seem to generate random numbers. |
| | Insert this script into your place:<br/> |
| <pre> | | <pre> |
| for i = 1, 10 do | | for i = 1, 10 do |
Line 10: |
Line 13: |
| </pre> | | </pre> |
|
| |
|
| Test the place, and look at the [[Scripting|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).
| | == See Also == |
| | |
| '''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).<br/>
| |
| 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! <br/>
| |
| 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 atleast 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!
| |
| | |
| === Example 1 ===
| |
| | |
| Have a script in workspace that checks for number of players in the game every 10 seconds or so, then does a math.random() code based on the number of players.<br/><br/>
| |
| | |
| <pre>
| |
| while true do
| |
| players = game.Players:getChildren()
| |
| wait(10)
| |
| if #players > 0 then
| |
| math.random(1, #players)
| |
| end
| |
| </pre>
| |
| | |
| "#players" means "number of players". We don't need to remember the random number we generate. It is enough to just generate a number to make the random number generator randomized. Also note, that if the number of players in a place is the same at all the same times every time, then the random numbers will be the same every time too.
| |
| | |
| === Example 2 ===
| |
| | |
| ''You don't really need to do anything more than in Example 1, but these extra examples are included for the more interested people.''<br/>
| |
| This script is even more randomized, and even randomizes the time between each randomization:
| |
| <pre>
| |
| while true do
| |
| players = game.Players:getChildren()
| |
| wait(math.random(1,#players))
| |
| if #players > 0 then
| |
| math.random(1, #players)
| |
| end
| |
| </pre>
| |
| | |
| Also here, note, that if the number of players in a place is the same at all the same times every time, then the random numbers will be the same every time too.
| |
| | |
| === Example 3 ===
| |
| ''You don't really need to do anything more than in Example 1, but these extra examples are included for the more interested people.''<br/>
| |
| Alternatively, you could randomize numbers based on players movement by putting this touch-script into a brick that players walk across now and then (maybe a baseplate?)
| |
| <pre>
| |
| local debounce = 0
| |
| function onTouch(hit)
| |
| if debounce == 0 then
| |
| debounce = 1
| |
| if hit.Parent:findFirstChild("Humanoid") ~= nil then
| |
| math.random(1,100)
| |
| end
| |
| debounce = 0
| |
| end
| |
| end
| |
| script.Parent.Touched:connect(onTouch)
| |
| </pre>
| |
|
| |
|
| Everytime someone steps on this brick, the random-number generator will be randomized.
| | [http://en.wikipedia.org/wiki/Random_number_generator Random Number Generator] |
|
| |
|
| [[Category:Scripting Tutorials]] | | [[Category:Scripting Tutorials]] |
Random number generator
The random number generator in Roblox does seem to generate random numbers.
Insert this script into your place:
for i = 1, 10 do
print(math.random(1,100))
wait(1)
end
See Also
Random Number Generator