Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make random generation with percentage?[SOLVED]

Asked by 5 years ago
Edited 5 years ago

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.

0
Not the solution, but a tip - you should Initialize your Gen variable at least once, BEFORE your While loop. JasonTheOwner 391 — 5y
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 Rafii2198 56 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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.

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 Rafii2198 56 — 5y
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... Rafii2198 56 — 5y
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. JasonTheOwner 391 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

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
0
Oh this is interesting! Cool. JasonTheOwner 391 — 5y

Answer this question