Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I acquire a certain “random range” in a higher frequency?

My question is basically, "how can I obtain certain random values within a specific range more than random values outside the range?"

Allow me to demonstrate what I mean:

If I were to, on a good amount of trials, start picking a variety of random numbers from 1-10, I should be seeing more numbers in the 7-10 range than in the 1-6 range.

I tried a couple of ways, but I am not getting desirable results.

#First Function:

    function getAverage(i)
    	math.randomseed(os.time())	
    	local sum = 0;
    	for j = 1,i do
    		sum = sum + (1-math.random()^3)*10
    	end
    	print(sum/i)
    end
    
    getAverage(500)

I was constantly getting numbers only around 7.5, such as 7.48, and 7.52. Although this does indeed get me a number within my range, I don't want such strict consistancy.

#Second Function:

    function getAverage(i)
    	math.randomseed(os.time())	
    	local sum = 0;
    	for j = 1,i do
    		sum = sum + (math.random() > .3 and math.random(7,10) or math.random(1,6))
    	end
    	print(sum/i)
    end
    
    getAverage(500)

This function didn't work as I wanted it to either. I primarily getting numbers such as 6.8 and 7.2 but nothing even close to 8.

#Third Function:

    function getAverage(i)
    	math.randomseed(os.time())	
    	local sum = 0;
    	for j = 1,i do
    		sum = sum + (((math.random(10) * 2)/1.2)^1.05) - math.random(1,3)
    	end
    	print(sum/i)
    end
    
    getAverage(500)

This function was giving me slightly more favorable results, with the function consistently returning 8, but that is the issue - consistency.

What type of paradigms or practical solutions can I use to generate more random numbers within a specific range over another range?

1 comment

0

You could make it math.random(1,15), then if it's >= 11, you change it to math.random(7,10).

1 answer

Your first idea sum = sum + (1-math.random()^3)*10 would be fine. However, really you're wanting to scale that down a bit. Try sum = sum + (1-math.random()^1.5)*10 instead and see how that works out for you.

This discussion is preserved as it was. New answers, comments, and votes are closed.