Help with drawing numbers at random

Hi there, This might be pretty confusing and weird but i’ll try to explain it as best I can.

I am trying to make system where a player clicks a GUI button and it loops randomly picking a number from 1 to 1,000,000 but the higher the number the harder the chance, if that makes sense. Ex: the number 400 would be 1/400 chance to get and the number 1,000,000 would be 1/a million chance to get.

Sorry if its confusing but any help at all is very much apricated.

So the number 1 is ‘1 in 1’ chance to get?

Yeah that is the idea. Although that would make it a 100% chance to get, if its not possible then it could just be 2-1,000,000.

To make this easier to understand picture it like spinning a wheel where there is a 50% chance you’ll land on a 2 or a 1% chance you’ll land on 100 and so on until 1,000,000 and if possible a way to implement a luck multiplier so it makes it easier the higher the players luck multiplier.

Perhaps you could do something like this

local min = 1
local max = 1000000
local exponent = 5 -- increasing/decreasing this should make the random number weight towards the lower/higher side

local randomNum = math.floor(min+(max-min)*math.random()^exponent)

I dont think that script works quite how I would like. I got these numbers after clicking it a few times and i dont think I got this lucky lol. for example the last number is meant to have a 1/649,378 chance to land on it (if the luck multiplier was at 1).

image

The probability distribution you’re describing is impossible, as if 2 has a 50% chance and 3 is 33% and 4 is 25% it’s already more than 100%.
This is the simplest implementation i can think of to give the distribution I think you want.

local min = 1
local max = 1000000
local ranNum = math.random(min, math random(min, max))

Edit: the best chance is for 1 at 1 in 1million. The worst chance is for 1,000,000 at 1 x 10^12

1 Like

As Wigglyaa said, the distribution your describing isn’t possible. Increase the exponent variable to something like 10, & play around it until you find something you like.

1 Like

I see, thank you. I might just write random odds for like 200 different numbers then.

Okay Im sorry to disturb you again but what about something like this? These numbers don’t add up to 100 but there’s still this chance that youll get the pet. How would something like that be achievable?

["Common Egg"] = {
		Cost = {"Coins", 100};
		IndexPos = 1;
		Backup = "Dog";
		ProductID = nil;
		ProductID2 = nil;
		Rarities = {
			["Cam"] = {80};
			["Part"] = {40};
			["Space Hex"] = {0.01};

Where you have relative probabilities your function will need to convert them first.
So when you divide the rarity by the total of all the rarities. This will give a value between 0 and 1.
Multiply this by 100, to get the actual percentage probabilities for each rarity.
You can then compare this to math.random(0, 100) to get your drawn number. [Edit: More accurate is math.random(1, sumofweights)]
Iterate through each converted rarity, if the drawn number is below the probability, you win the item, if not, subtract the rarity probability from the drawn number and repeat.

There are probably other (and better) ways to do this, but this is what I use when I use weighted chances (as I can afford to ignore rounding errors and the smallest weight is 1, as math.random only returns integers).

1 Like