- Home /
Easy way for shuffle a non-repeatable list
After 4 days of research on ways to shuffle lists, seeing old theories like Fisher-Yates and others saw to that Unity is not hard as well.
I simply did a raffle same in real life. In real life a draw is made by a ball full of other small balls with numbers. And the man or woman turns and goes catching small balls one by one, and placing aligned on the table.
Let me give an example, I have 60 small balls inside a cage, I want to give away 6 small balls, then I turn this "cage" to fall 6 small balls with various numbers.
6 These balls were drawn randomly and without order.
In programming is that foundation that I used (C# Example). Using System.Collections.Generic;
//First create the "cage" with 60 small balls on some void().
List<int> smallBalls = new List<int>{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60};
//Now i create the "table" for 6 random small balls
int[] randomBalls = new int[6];
//Now let's try pass the 6 smallBalls to table, look on "for loop i typed 53, because i need 6 balls, so "59 - 6 = 53"
try{
for(int i = 0; i <= 53; i++){
//for catch 6 random number of the "cage" i use Random.Range, and REMOVE Unsorted numbers...
smallBalls.RemoveAt(Random.Range(0,smallBalls.Count));
}
//Now the "cage" have 6 smallballs with non-repeatable random numbers.
for(int z = 0; z <= 5; z++){
//Geting all smallsballs and passing to int array
randomBalls[z] = smallBalls[z];
}
}
catch(System.ArgumentOutOfRangeException e){
return;
}
Now your have the randomBalls Array with 6 non-repeatable random numbers :D This also works for objects, textures, and among others that fit within a list and arrays.
Well it's not literally a question, but if you also have a better example or want to supplement my, please feel free.
Too many people wanting to make drawings of numbers that does not repeat.
Your answer
Follow this Question
Related Questions
Cards: Stacking, drawing, hand, and shuffling 1 Answer
How do I shuffle a list and reset it back to the previous ordering later? 1 Answer
ANSWERED [C#] Coding Noob here- could somebody help with this short shuffling program? 1 Answer
Match 3 Fill Match Gridlock Logic Help 0 Answers
Shuffle Array on button 2 Answers