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

How do I make a random value generator?

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

✓ Accepted answer

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

11 comments

0

Then is a keyword, and does not appear in blue. And line 4 brackets are redundant

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

0

wait then whats the point of math.random() then? D:

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

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

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?

0

@Phlegethon5778 they already did a while ago

0

@RiskoZoSlovenska if you don't supply a seed, it will pick a random seed

0

then does NOT show up in blue.

0

@incapaz you might have changed your colors but it indeed does. https://gyazo.com/d4724cd1cb15e832a55562cf906678b0

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

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

1 comment

0

You should start your script off with math.randomseed(tick()) to make it more random.

@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

3 comments

0

That's what I told him lol

0

I was typing this answer and when I posted it ur comment appeared too

0

Thanks to all of you :D

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