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

How to make random generation with percentage?[SOLVED]

I am making randomly generated terrain that is made of 7x7x7 blocks. Each of them has percentage that defines its chance to spawn(max chance is 1000 and min is 1). The problem is that this is not working properly, for example: when something has 999 of chance and other this has 1 chance and then run the script there still be many second blocks. The code for picking a block to generate looks like it:

function Precentage()
while true do
	Place = math.random(1,#Minerals)
	Blocks = Minerals[Place]
		if  Blocks.prc.Value >= Gen then
			original = Minerals[Place]
			break
		else
			Gen = math.random(1,1000)
			Place = math.random(1,#Minerals)
		end
	end
end

The 'prc value' is the percentage of the block.

I just run the script and I saw a field that has as many blocks which have 26 percentage as the block which have 999 percentage.

2 comments

0

Not the solution, but a tip - you should Initialize your Gen variable at least once, BEFORE your While loop.

0

I have: "local Gen = 2" at the beginning of the whole script as well as all other values similarly. This is just part of selection of a block which makes the problem

2 answers

The problem here is you're not in control with the percentage of times the math.random function will pick a certain number. It might pick the number 42, 100 times, even though you have a wide minimum and maximum set - you and I didn't program that function, so how can we trust it, as far as percentages go?

What you might consider doing is creating a function that counts how many times a number is picked, in the range of 1,1000, and run it in a loop of maybe 10,0000 times, and then have it display the results - that may give you a better idea on how the random number generator is performing.

Once you know which numbers or number ranges come up, you can add IF.... THEN conditions to determine which items to spawn.

3 comments

0

Oh i forgot to add it: what I showed is not whole script as it has 60 lines and most of them are not related to that problem... But I have module script called Randomizer and it randomizes the seed for math.random but I'll look into your proposition and then prepare conclusion

0

Ok so I made that test and literally none of the numbers was generated more than 20 times and the lowest number it got was 3 although I saw it pop 2 a few time and literally I saw number 1 only 2 times so it is fairly random so the problem is in my picking process not in math.random...

0

Okay, that's great that you tested this. I have had a need for a percentage based thing for minerals before, as well. I'll post what I did.

So apparently I changed some stuff and it fixed the problem

function Precentage()
while true do
	Randomizer.Randomizer()
	Gen = math.random(1,1000)	
	Place = math.random(1,#Minerals)
	Blocks = Minerals[Place]
		if  Blocks.prc.Value >= Gen then
			original = Minerals[Place]
			break 
			else
			end
	end
end

1 comment

0

Oh this is interesting! Cool.

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