C# No Repetition of number<<plz help nid for THESIS>> :'(

i have a problem, i came up to different algorithm,but it seems nothings work,

what i need is an array for example:
string[ ] ques = new string[10];

for(int i = 0;i < 10; i++){

ques = Random.Range(0,9);
}
problem is how can i compare if all the
value inside the array is unique??
tnx in advance :expressionless:

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 dont get it…can you give a sample code…at least up to 5 arrays only…plss :frowning:

No.

Please answer my other questions first.
Isn’t this basic stuff to be asking, since you’re writing a thesis?
How come you need this part?

And can’t you use a HashTable as an arraytype? that is already distinct… (may not contain duplicates)

… Write my thesis ftw!

im a computer science student and we are making a 3d game…i need the no repetition code for my game…

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.

I never would’ve guessed that!

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)

Hi Jorine,

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.

Thank you so much with all your help, i will do all what you said and i will let you know if im done…
Annihlator,shawndg, and ligthningGamer
:smile: :slight_smile: :))

HashSet. Works like a List but prevents duplicate values and uses a hash to perform searches faster.

Also - you’re a CompSci student. Spend time writing your own code instead of begging for canned solutions on a message board.

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.

# generate a list from 1 to 20
a_list = [x for x in range(20)]
# sort the list randomly
a_list.Sort({x, y | return Random.Next(-1, 2)})

:slight_smile:

something as simple as this should work. probably not code correct, but enough to give you and idea

for(int i = 0;i < 10; i++){

   int value = Random.Range(0,9);
   while(!ques.Contains(value))
   {
     value = Random.Range(0,9);
   }
   ques[i] = value;
}

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:

  1. Generate an ordered list of the entire range.
  2. Shuffle that list.
  3. 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?

I think that you’ll find using ‘orderby’ will give you a statistically more random list :stuck_out_tongue:

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

i have my code now …

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,

ug thats more horrid than the code i wrote.

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)