Either by looping through every single listing in the array, comparing it against all the others, or by making an array “distinct” (You should be able to achieve this through LinQ, however i am not sure how as i never applied LinQ myself.)
However, this is discussed over here: http://stackoverflow.com/questions/4162843/c-sharp-array-how-to-make-data-in-an-array-distinct-from-each-other
The easiest solution would be picking a listtype that’s distinct anyway (i.e.; a HashTable is Distinct)
(Isn’t this however basic stuff to be asking, since you’re writing a Thesis?)
I’m new to coding so this might not be right, but couldn’t you just get an if statement and have it take the value you randomly got (i) and compare it to the other values that have been assigned so far, and if the i == to another value tell it to randomly assign it again and check that value etc etc. I doubt that is the most efficient way to do it but I think it would still work.
now, all sarcasm aside.
There’s no exact thing as no-repetition code. (okay, there probably is. but please assume that’s much too resource hungry and complicated)
You need to implement a non-repititive way to use your code, which is completely different.
Still the best suggestion i have is to choose a suitable arraytype… since you don’t want any repetitions in this array, that would mean to use a “distinctive” array (Allowing only Distinct/Unique values).
A basic array tutorial (C#) is here; http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
however that doesn’t explain distinct arrays.
An example how you can use linq (theoretically) to keep an array distinct is here:
it even includes an example!
The power-hogging option is for every added value, have a for-statement check that value against all other values in your array, only allowing it to be added once it checked your new value against the whole array (time consuming as more and bigger arrays are used, thus might not be a good option)
Try this, sorry I am working right now and between launch and the rest of the day so I cant write much in the way of examples…
but here it is untested…
First you need a for loop…
loop while you need values…
Then inside your loop you fill in the values…
maybe in array but before you fill them in…
you loop through the array to make sure the value is not in use… if it is then run the generator again.
a small range of values… i would not worry much about it running… in theory yes it could run for ever but it wont if its a small range.
It will always exit with a list of unique values in a random order.
I would like to add that you could use a List array filled with the range of numbers you need.
Then in your for loop you random from 0 to list length, retrieve the number at that index and remove it from the list.
Generating a new number until you find one that isn’t in the list is about as slow as you can get.
For starters List.Contains is slow because the list isn’t ordered—worst case you have to traverse the entire list on every Contains call.
Secondly, the number of calls to Random.Next() for position n is going to be about n times (for example, if the list is a million items long and you are populating the last position, you’ll have to call Random.Next() around a million times to stumble upon the correct final number you need).
The answer depends on how big the range you’re selecting from is, compared to how big the set of numbers you’re generating is.
If the two are similar (or identical), as in the OP, then the simplest solution would be:
Generate an ordered list of the entire range.
Shuffle that list.
Chop it down to the desired size.
If the range of possible numbers is significantly larger (getting 10 numbers between 1-100, for instance), then you could use the method of generating numbers in range and seeing whether they’re already in the list (as in JamesLeeNZ’s example). Using a sorted collection could speed things up here if necessary.
Also - why are you using strings to store numbers?
If you know what pool of choices you are choosing from, eg, a bag of colored beads,
you can start with an ordered list (red,green,blue,yellow)
then remove a random one from the bag, and put it into a different list.
repeat until ordered list is empty.
the second list will now be in a random order
this is the tetris solution
for (int i=0;i<10;i++)
{
bool check; //variable to check or number is already used
int n; //variable to store the number in
do
{
n=Random.Range(0,9);
//check or number is already used:
check=true;
for (int j=0;j<i;j++)
if (n == ran[j]) //if number is already used
{
check=false; //set check to false
break; //no need to check the other elements of value[ ]
}
} while (!check); //loop until new, unique number is found
ran*=n; //store the generated number in the array* } //output
for(int r = 0;r<10;r++){*
print(ran[r]);*
}* ive tested this in DevC++ and it works but when i try this in unity unity3d is hanging,
The reason its freezing (im assuming - cbf trying to understand ur code completely) is because Unity Random.Range (int), is exlusive of the top value, therefore it can never complete. Make your Random.Range (0, 10)