Reputation: 689
There is a ton of examples of generating random numbers in LUA that have no duplicates, and just a standard math.random(x,y) can get a set of random whole numbers in a range....
... but I am having trouble finding a set of random numbers between a range, but allowing x amount of duplicates. For my immediate needs I can allow 1 set of duplicates, but it would be great to have code where you can set "duplicate value" to anything for future projects.
Example : I want to generate a list of 10 whole numbers between 1-10... each value can be anything between 1-10, but any one number can only be generated and added to the list twice.
Example Result: 1,1,2,4,5,5,7,7,8,9
In this example result math.random() tried to spit out 3 or more of the same number, but the code makes it go back and try again if it has already produced 2 of the same number.
Thanks in advance!
Upvotes: 1
Views: 446
Reputation: 689
Upvotes: 0
Reputation: 562
You can use "merge trick":
Create "unical" array of numbers for 5 (10/number of dublicats) elements: 1,2,5,7,9
Repeate #1
Merge arrays.
You can generalize it with paramers of minValue, maxValue, totalNumber, numberOfDublicates, but will need to little more code for handling 10/3 problems and maxValue < totalNumber.
Upvotes: 1