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

Selecting randomly from known probability?

Asked by 6 years ago

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?

0
Use math.random and covert each number to a string. For different raritys, have several numbers equal one string Viking359 161 — 6y
0
math.random isn't actually random. awfulszn 394 — 6y
0
You could create a random number from 0 ~100 and use that as a %? User#5423 17 — 6y
0
How would I manipulate the randomly generated number though? shadow7692 69 — 6y

1 answer

Log in to vote
1
Answered by
Viking359 161
6 years ago
Edited 6 years ago

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.

0
This doesn't work - no output, no errors. shadow7692 69 — 6y
0
Make sure this is a server script, and where is the script? Viking359 161 — 6y
0
It is in a server script, in the Server Script Service shadow7692 69 — 6y
0
I rewrote the script, it should work now. Viking359 161 — 6y
Ad

Answer this question