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

How can i make a randomiser have a range between 0-100?

Asked by 5 years ago
Edited 5 years ago

I have created a simple random script that changes numbers every second.

local infLoopNum = 1
while infLoopNum == 1 do
randomNum = math.random(1,100)
print(randomNum)
wait(1)
end

I know its 6 lines, but I cant figure it out.

2 answers

Log in to vote
0
Answered by 5 years ago

Ok, there are a couple of thing that can be improved here, for one the infLoopNum is not necessary, as a simple while true do loop works just fine. Second, it is highly encouraged to use a math.randomseed(tick()) before doing anything with math.random(), with all things said, the final result should look similar to:

math.randomseed(tick())
while true do
    randomNum = math.random(0,100)
    print(randomNum)
    wait(1)
end
Ad
Log in to vote
0
Answered by 5 years ago

How to make Random numbers

Although theking48989987's answer is valid, there is a faster, and better way to make random numbers in your game.

Random vs. math.random()

Using the userdata, Random, added in 2017, is much faster than using math.random(). This also encourages you to use Random seeds instead of optionally using seeds. Random is faster than math.random because it Random uses a different, faster algorithm(the difference is significant).

How to use Random numbers!

Take a look at the code below!

Code:

local RandomSeed = Random.new(tick()) -- Like math.randomseed(tick())

while true do
    -- Creates a Random integer between 0 and 100
    local RandomNumber = RandomSeed:NextInteger(0,100)
    print(RandomNumber)
    wait(1)
end

Hopefully this helped!

Answer this question