How would i make a script that generates a random number and the lower the number is the rarer it is to get. Example:
5 = 1/500
50 1/5
500 = 1/2
I know kinda bad example tough.
How would i make a script that generates a random number and the lower the number is the rarer it is to get. Example:
5 = 1/500
50 1/5
500 = 1/2
I know kinda bad example tough.
Combining multiple math.random() functions should give you some sort of a working system.
Here’s a simple example:
local n = math.round(math.random(1,100) ^ (1 + math.random()))
Take note that extremely large numbers are also pretty rare so you could add an if to the script to filter out bigger numbers.
Here’s a bit more complex way to calculate such numbers:
local m= 100
local b = 2
local n = math.round(1 - b ^ (math.log(b;m) - math.random(1, 10)))
m is the biggest possible value
the other numbers just change the graph a bit
You can change the numbers to whatever is into your liking. The range of the math.random() function should be changed if you wish to use larger numbers.
you could do a weighting system.
local weights = {
5 = 0
50 = 10,
500 = 1000,
}
local rand = math.random(0,1000)
local result
for i,v in pairs(weights)do
if rand > v then
result = i
end
end
So basically each of the numbers has a weight held in the weight table, we then generate a random number, if that number is bigger than the weigh then that weight’s index becomes the result, we then work through the weights table replacing the result untill the weight becomes bigger than the random then we end the program.
So with the current weights ive given them 5 will have a 1/100 chance, 50 will have a 99/100 chance and 500 will have a 1/1000 chance
I think wolfy requested it the other way around, smaller numbers being rarer than larger ones
But hey, that’s just a minor detail that can be changed pretty easily
This seemed to work, I’m barely getting any low numbers out of this. But it’s going over 100.
I’m printing n
repeatedly, and this is what I’m getting:
This is just giving errors:
oh yes change that ; into ,
mb
Now it just returns one’s and zero’s.
function spread(max)
local x = math.random()
return math.floor(max * (1 - x^2))
end
while true do
task.wait(0.33)
local pick = spread(100)
print(pick)
end
function spread(min, max)
local x = math.random()
local spreadValue = math.floor(min + (max - min) * (1 - x^2))
return math.max(min, math.min(max, spreadValue))
end
while true do
task.wait(0.33)
local pick = spread(1, 100)
print(pick)
end
The lower script set up as
local pick = spread(1, 10)
really shows the low picks are rare.
This is a simple trick here. I’ve tired many, this is harder than it seems.
The best one (most work put in) I ever made ended up being about the same as this one doing it this way. Random is just odd to work with.
I think that worked! The number has not gone over the max, and I’m barely getting low numbers
I wont mark it as solution yet, I will test it more (by waiting).
How do i set the minimal value tho?
oh yes here’s the working script
local m= 100
local n = math.round(m - m ^ ( 1 - math.random(1, 10)))
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.