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

How do I make a random value generator?

Asked by 5 years ago

Hi, I'd like to know how to make a randomized value. More specifically, a randomized true/false value, but random in general would be nice to know how to do. I would like it to be completely random each time the script is run.

It's just a quick question I'm sure one of you can answer :D Thanks for your time!

3 answers

Log in to vote
0
Answered by
MrMedian 160
5 years ago

There is actually a new datatype called Random. You can see this in the script editor because it will show up blue, just like print and then. To make a new Random generator, you use the .new() method of the Random datatype.

local RNG = Random.new()

Now, there are two different methods to call on the new random number generator object. First is :NextInteger(LowNumber, HighNumber). This will give you an integer value between the Low and High numbers you provide it.

local RandomInt = RNG:NextInteger(0, 10) -- a random integer (no decimals) between 0 and 10

The second is :NextNumber. This gives you an number value between the Low and High you provide it.

local RandomNum = RNG:NextNumber(0, 10) -- a random number (with decimals) between 0 and 10

To get a random boolean value (true or false), this can be used:

function GetRandomBool()
    local RNG = Random.new()
    local Int = RNG:NextInteger(0, 1)
    return (Int == 1)
end
0
Then is a keyword, and does not appear in blue. And line 4 brackets are redundant User#19524 175 — 5y
0
first of all, then does show up in blue. second, you don't need to keep calling out small little things that you may not do. I personally use parentheses to make it more readable MrMedian 160 — 5y
0
wait then whats the point of math.random() then? D: RiskoZoSlovenska 378 — 5y
0
basically math.random is the "old" way of getting a random number. Plenty of people still use math.random, so it's basically up to you which one you prefer. I'm pretty sure the Random datatype offers more flexibility tho MrMedian 160 — 5y
View all comments (7 more)
0
Roblox will make the math.random algorithm the same as the Random datatype soon. If you read the whole wiki article you will see what I say is true User#21908 42 — 5y
0
and what about the generator seed for the Random? Does it automaticaly use a different one each time? Also, does this also mean that math.random() will be deprecated and I should use Random? RiskoZoSlovenska 378 — 5y
0
@Phlegethon5778 they already did a while ago MrMedian 160 — 5y
0
@RiskoZoSlovenska if you don't supply a seed, it will pick a random seed MrMedian 160 — 5y
0
then does NOT show up in blue. User#19524 175 — 5y
0
@incapaz you might have changed your colors but it indeed does. https://gyazo.com/d4724cd1cb15e832a55562cf906678b0 MrMedian 160 — 5y
0
Uhh thanks to all of you for your input xD this is a heck of a lot of answers and comments for such a small question :P but thank you all anyways :D Juolite395 11 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I'm assuming you mean something like this possibly

local Randomized = math.random(0,1)
local BoolThing = nil
if Randomized == 0 then
    BoolThing = false
elseif Randomized == 1 then 
    BoolThing = true
end
0
You should start your script off with math.randomseed(tick()) to make it more random. User#19524 175 — 5y
Log in to vote
0
Answered by 5 years ago

@StayToasted the problem with your script is that i'll return the same values everytime its run. Random generation on computers works like a function - it returns the same values when the same parameters are inputed. So what we need to do is change the parameter of the math.random function, so that its different every time its called. The function to do this is math.randomseed.

But what do we put into here if we want a number that is different every time? Well, we can enter a few different values. I prefer to use tick(). tick() returns the number of seconds that passes since January 1, 1970, so its different everytime its called.

So StayToasted's script should look like this:

math.randomseed(tick())
local Randomized = math.random(0,1)
local BoolThing = nil
if Randomized == 0 then
    BoolThing = false
elseif Randomized == 1 then
    BoolThing = true
end
0
That's what I told him lol User#19524 175 — 5y
0
I was typing this answer and when I posted it ur comment appeared too RiskoZoSlovenska 378 — 5y
0
Thanks to all of you :D Juolite395 11 — 5y

Answer this question