How can I make a random chance system?

I want to make it so when a player does something there is a 1% chance that it will fire an event how can I do this? I’ve searched around most methods talk about pseudorandom being a problem and some require you to have multiple items for it to work.

2 Likes

Well math.random(min,max) gives you a random number so you could just do something like this.

if math.random(1,100) == 1 then
    --Do what you need a 1% chance for here
end

This was my approach I was curios if there was a better way but I used a ‘True Random’ module because math.random is only pseudo-random.

local counter = 0
local rarity_min = 1
local rarity_max = 100

local TRUE_RANDOM = require(game.ReplicatedStorage.TrueRandom)
TRUE_RANDOM.DefaultListLength = rarity_max
local rng = TRUE_RANDOM.new(rarity_min, rarity_max)

for _, n in ipairs(rng._Numbers) do
	if n <= 1 then
		counter += 1
	end
	print(n)
end
print('_________________________________________')
print(counter)
1 Like

Nothing is truly random (in terms of programming randomness), only pseudo-random.

Consider using the “Random” class instead.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local RandomObject = Random.new(tick())
local RandomNumber = Random:NextInteger(1, Player.UserId)
print(RandomNumber)

The above snippet generates a random number between 1 and the player’s UserId inclusively.

3 Likes

Wouldn’t this theoretically just make players with lower UserIds luckier (assuming you check if a number is less than a threshold)? Why not just have a set cap? It makes it easier to use.

Also, using Random objects are psuedo-random, but the true random module they use uses HTTP requests to get a true random number from the web, so there actually is a benefit in using it if you need true randomness.

1 Like

It’s just an example script of the “Random” class in-use, akin to a documentation example.

Yes, the Random class like the math library function random() is pseudo-random.

1 Like

According to random documentation, the random class is still pseudo-random. This is a web API using random.org to generate truly random numbers also I have fully solved the problem I am now able to simulate 100 random numbers then I check if the number is equal to or lower than the percent I give.

1 Like

Yes most randomness on computers is pseudorandom instead of true random, but for the most part pseudorandom number generators are good enough that you won’t really be able to tell the difference.

A good deal of research has gone into pseudo-random number theory, and modern algorithms for generating pseudo-random numbers are so good that the numbers look exactly like they were really random.RANDOM.ORG - Introduction to Randomness and Random Numbers

Realistically unless you are dealing with a situation in which you can’t allow the result to be predetermined, pseudorandom is usually going to be good enough.

Using true random however you have the drawback of introducing latency to the numbers you generate since you have to talk to another server to get your numbers with practically no advantage for your use case.

CloudFlare emulates true randomness through the use of lava lamps.

1 Like

you are correct about this but all I have to do is preload 100 numbers then check each number in the list instead of getting each number individually I can drop the module here if you wanna take a look here’s something I made with it;


I’m a moderate scripter and the module works but I don’t know if my logic is 100% correct so if you want you could double-check me.
https://www.roblox.com/library/8688359713/rngSystem