- Home /
The question is answered, right answer was accepted
How to generate unique random number
hi , i am new in unity , but i work at IOS cocos2d and sprite-kit. i made unique random number at cocos2d the code are given below , but i just cant make that thing convert properly at unity
here is the code at objective c
-(void)CreateRandomCircle{
int Range = 20;
int random;
int checker[Range];
for(int i = 0 ; i < Range ; i++)
{
checker[i] = 0;
}
for(int i = 0 ; i < Range ; i++)
{
random = arc4random() % Range;
while(checker[random] == 1)
{
random = arc4random() % Range;
}
checker[random] = 1;
NumArray2 [i]=random;
}
}
but i tried to make it in unity here is my try
private int randomm;
private int[] checker = new int[];
private int[] UnicNumArray = new int[20];
void CreateRandomCircle()
{
checker = new int[20];
for (int i = 0; i <20 ; i++)
{
checker [i] = 0;
randomm = Random.Range (0, 20);
print(randomm);
while (checker[randomm] == 1)
{
randomm = Random.Range (0, 3);
}
checker [randomm] = 1;
UnicNumArray [i] = randomm;
}
}
please this will be a big time help .
again all i want is 20 unique random number at one single array
So you want an array of 20 elements, with unique random numbers in there? And those numbers range from 0 to 19?
You have to generate random numbers and save in an array.. and compare each time with previously generated random number using a loop and if-else condition.
20 unique random number between 0-20? integer or floating point number?
You'd better generate straight array of numbers from 1..19 and then shuffle it by moving randomly picked numbers from it in new array. This way worked nice when i used dynamic arrays in JS and picked using Array.length*$$anonymous$$ath.Random() as index.
2 searchable terms for use in answering this question:
ShuffleBag
Random Seed
Answer by Anxo · May 27, 2014 at 02:56 PM
Something like this? I have not tested it but this should give you a list of int from 0 to 19 in a random order. If I understood your question correctly.
using System.Collections.Generic
private int maxNum = 20;
private List <int> randomList;
public void GenerateRandomList (){
for(int i = 0; i < maxNum; i++){
int numToAdd = Random.Range(0,maxNum);
while(!randomList.contains(numToAdd)){
numToAdd = Random.Range(0,maxNum);
}
randomList.Add(numToAdd);
}
//Done.
}
Oh do not forget
void Start(){
randomList = new List<int> ();
}
And a cleaner version might actually be something like this ins$$anonymous$$d.
private int maxNumbers = 20;
private List<int> uniqueNumbers;
private List<int> finishedList;
void Start(){
uniqueNumber = new List<int>();
finishedList = new List<int>();
}
public void GenerateRandomList(){
for(int i = 0; i < maxNumbers; i++){
uniqueNumbers.Add(i);
}
for(int i = 0; i< maxNumbers; i ++){
int ranNum = uniqueNumbers[Random.Range(0,uniqueNumbers.Count)];
finishedNumbers.Add(ranNum);
uniqueNumbers.Remove(ranNum)
}
//Done.
}
I think this is cleaner b/c it leaves less things to chance.
for the first solution, line 10 is not suppose to have the "!" symbol right? because we want to gen a new number if it DOES contain it.
Follow this Question
Related Questions
Random texture changer (problem with array) 2 Answers
random number generator problem 1 Answer
How to prevent picking the same combination in array? 1 Answer
Problems with array "shuffle bag" 1 Answer
problem with random array 1 Answer