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

Selecting randomly from known probability?

I have these four variables:

local RarityBasic		= 0.4 -- The probability.
local RarityRare		= 0.3
local RarityLegendary	= 0.2
local RarityExotic		= 0.1

Where the number is the probability. I want a way of randomly choosing one of them taking into account the probability that each thing has.

The output would be something like:

print("Rarity: "..Rarity)
>> Rarity: Basic

How would I do this?

4 comments

0

Use math.random and covert each number to a string. For different raritys, have several numbers equal one string

0

math.random isn't actually random.

0

You could create a random number from 0 ~100 and use that as a %?

0

How would I manipulate the randomly generated number though?

1 answer

✓ Accepted answer

Do something like this :

math.randomseed(tick())
local number = math.random(1,10)
if number == 1 then
print("Rarity : Exotic")
elseif number >= 2 and number <=3 then
print("Rarity : Legendary")
elseif number >= 4 and number <=6 then
print("Rarity : Rare")
elseif number >= 7 and number <= 10 then
print("Rarity : Basic")
end

It basically assigns different chances to each rarity so the exotic has a 10% chance, legendary has 20% chance, etc.

4 comments

0

This doesn't work - no output, no errors.

0

Make sure this is a server script, and where is the script?

0

It is in a server script, in the Server Script Service

0

I rewrote the script, it should work now.

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