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 7 years ago

I have these four variables:

1local RarityBasic       = 0.4 -- The probability.
2local RarityRare        = 0.3
3local RarityLegendary   = 0.2
4local 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:

1print("Rarity: "..Rarity)
2>> 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 — 7y
0
math.random isn't actually random. awfulszn 394 — 7y
0
You could create a random number from 0 ~100 and use that as a %? User#5423 17 — 7y
0
How would I manipulate the randomly generated number though? shadow7692 69 — 7y

1 answer

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

Do something like this :

01math.randomseed(tick())
02local number = math.random(1,10)
03if number == 1 then
04print("Rarity : Exotic")
05elseif number >= 2 and number <=3 then
06print("Rarity : Legendary")
07elseif number >= 4 and number <=6 then
08print("Rarity : Rare")
09elseif number >= 7 and number <= 10 then
10print("Rarity : Basic")
11end

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 — 7y
0
Make sure this is a server script, and where is the script? Viking359 161 — 7y
0
It is in a server script, in the Server Script Service shadow7692 69 — 7y
0
I rewrote the script, it should work now. Viking359 161 — 7y
Ad

Answer this question