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

Chance of occurance?

How would you go about scripting a percentage? I understand this is not a request site, however I have no idea where to start so a few pointers would help a lot. I have made a script that something will happen if etc however I want a chance of that happening I don't want it happening everytime. Would I use Weights or not, thanks!

1 answer

✓ Accepted answer

A percentage would be a value between 0 and 1, so, for 75% we would have

chance = 0.75
-- or
chance = 75 / 100

To "roll the dice," (so to speak) we generate a random number between 0 and 1:

randomNumber = math.random()

That randomNumber will be less than chance chance amount of the time (e.g., a random number from 0 to 1 is less than 0.35, well, 35% of the time).

So that is our check:

if randomNumber < chance then
	-- only occurs with `chance` probability
end

(Or inline)

if math.random() < 0.75 then
	-- only occurs 3/4 of the time
end


#Explanation of using math.random with different arguments for this purpose:

math.random(a,b) returns a random integer between a and b (inclusive).

So, for 75% chance, we could use these:

	math.random(1,4) <= 3 
	math.random(1,100) <= 75

However, if we wanted to use 75.9%, this doesn't work:

	math.random(1,4) <= 3.036
	-- math.random(1,4) only returns 1, 2, 3, or 4.
	-- So this is no different than just `math.random(1,4) <= 3` (75%)
	math.random(1,100) <= 75.9
	-- Same problem: same as 75%

However, math.random() with no argument generates any number between 0 and 1.

Here's sample output:

0.84018771715471
0.39438292681909
0.78309922375861
0.79844003347607
0.91164735793678

It provides numbers as dense as possible, so we don't have the problems of only generating integers now.

	math.random() < .75
	-- works just as well as
	math.random() < .759

You might notice that this is actually pretty much equivalent to just dividing by the appropriate power of 10, e.g., for our 75.9% example, we could have used:

	math.random(1,1000) <= 759

However, that starts to get messy. Especially with long decimals. What if we wanted a 1 in math.pi chance?

if math.random(1,10000000000) <= 3183098861 then

which is just getting ridiculous (and is also not to the full precision it could be). Much more elegant to use:

	math.random() < 1 / math.pi

2 comments

1

But we would want Math.random to be from 0-1 won't we? So would we have to put math.random (0-100) and set chance to 75?

0

You can do that, too. math.random() (without any arguments) makes any number between 0 and 1 (not just 0 or 1). But if we used math.random(1,100), then we wouldn't be able to use 87.5%; it would actually be 87% likelihood. For between [0,1] we don't have that problem, since we can use math.random() < .875 just fine; Made a lengthy edit describing this more.

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