- Home /
unique list of 10 random numbers
Hey there unity Answers!
I'm trying to get 10 random numbers from 0 - 120, then store those numbers into a array (or list) only trick is, i keep getting duplicates. is there a good way to get the random number (random.range(0,120) then add it to an array, ONLY if it isn't already in there?
thanks in advance!
Answer by Eric5h5 · Nov 17, 2010 at 09:53 PM
You could have a list of all numbers from 0-120, then remove the ones you've already picked for the array:
function Start () {
var numbers = new List.<int>(121);
for (i = 0; i < 121; i++) {
numbers.Add(i);
}
var randomNumbers = new int[10];
for (i = 0; i < randomNumbers.Length; i++) {
var thisNumber = Random.Range(0, numbers.Count);
randomNumbers[i] = numbers[thisNumber];
numbers.RemoveAt(thisNumber);
}
}
is there a way to just add the number to a new list, if it isn't already in that list?
Didn't really understand that...is this something new? The code I wrote answers the original question.
sorry bout that. unless I'm reading the code wrong, it seems like it's going to remove the random numbers (thisNumber) from the array of numbers from 0-120. I was wondering if you can check if the random number (thisNumber) has been picked, and if not add it to a list of the random numbers it picked, until it reaches 10 unique ones.
actually, sorry again, I read the code wrong, got it now. :/ THAN$$anonymous$$S!
@vfxjex It doesn't take "a lot of processing", it's utterly trivial. It's just a loop using List.Add, and hardly takes more time than allocating the list itself (at least with a size that small).
Answer by IJM · Nov 17, 2010 at 09:54 PM
Every time you are adding a new random number to your list you sould go thro all numbers in the list to see whether that number alredy exists in the list. If it exists in the list, you ask for another one, if not you add it to the list.
Answer by poncho · Jan 06, 2011 at 10:58 PM
this is an other way to doit, my code suggestion would be this
void Start()
{
//having an int Array of 10 members
int [] numbers = new int[10];
//using a string variable to keep record of the used numbers
string usedNumbers ="-";
//cycle to get 10 random numbers
for (int i = 0 ; i<10; i++)
{
//get a random number between 0 - 120, 0 and 120 included on the random numbers
int randomNumber = Random.Range(0,121);
//Checking if the random number you get is unique, if you already have it in the string means that this random number is repeated, the "-X-" is to make the difference between the numbers, like -1- from -11-, if you dont have the "-X-" the 1 is in 21 and would be a "repeated" number
while(usedNumbers.Contains("-"+randomNumber.ToString()+"-"))
{
//if a number is repeated, then get a new random number
randomNumber = Random.Range(0,121);
}
usedNumbers+= randomNumber.ToString()+"-";
numbers[i] = randomNumber;
}
}
with this code you will never have 2 same numbers
hope the explanation of the code helps you
Your understanding of my code is completely wrong. It does exactly what the question asked, in a reasonably efficient way, and has no chance at all of getting the same numbers. Your code is brute-force, plus it has highly inefficient string conversions, comparisons, and concatenations. Also it doesn't scale well...for example it will perform quite badly if you wanted a unique list of 110 out of 120 random numbers, whereas $$anonymous$$e would still be just as fast, aside from looping 110 times ins$$anonymous$$d of 10.
yeah a few secs after posting i read again and it was correct XD sorry dude, thats why i changed my answer, just give this answer for another way to do it, about the performance, is quite stable, cuz the memory just need 1 byte for each char in the string, not needed to make the whole array, anyway i just gave an other way of thinking and provided how the logic was developed, for own understanding of the code, this means that he can learn from the code, not only copy paste sorry for the misunderstanding
No, chars are more than one byte since they're Unicode. Also, every time you use "+" to concatenate a string, it has to allocate another complete string, which ends up being extremely wasteful when you do it repeatedly. The garbage collector gets it back, but it's still a waste of resources and CPU time. Ins$$anonymous$$d of abusing strings that like that, it would be a lot better to simply check the actual contents of the array, but that technique still has the problem of not scaling well like I mentioned. It only works well if you have a small selection of random numbers out of a much larger set.
Answer by steddyman · Mar 09, 2013 at 12:17 AM
Recommend you take a look at Unity Random on the asset store. It has shuffle bags which would fix this. I use it for a similar scenario.
Unity Random? Link please. Sounds interesting. I found that poster "Eric5h5" did things perfectly but Unity Random might be worth a look for folk interested in this sort of thing.
Your answer
Follow this Question
Related Questions
Need some help with Array searching logic 1 Answer
Selection list from Array Unity - Random - GameObjects array 1 Answer
A node in a childnode? 1 Answer
Lists: Avoiding ArgumentOutOfRangeException: Argument is out of range errors 1 Answer
Creating a record of values generated with Random.Range 1 Answer