0

I have one list(planePosition). I want to use those list value randomly without repetition so I am using

 for( int a=0; a< planePosition.Count; a++)
    { 
    int r=Random.Range(0,planePosition.Count);
    Newplan.transform.position =  new Vector3 (planePosition[r].x, planePosition[r].y ,9.990011f);
    planePosition.RemoveAt(r);
    }

but still I am getting duplicate value..

mjhm
  • 16,497
  • 10
  • 44
  • 55
kajal
  • 13
  • 3
  • possible duplicate of [Randomize a List in C#](http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp) – CodeCaster Sep 25 '14 at 12:11
  • 1
    Do you mean to only use half the values in planePosition? That is the result of this for loop because `planePosition.Count` will decrease by one for each iteration because of the `planePosition.RemoveAt(r)` and of course `a++` is causing `a` to increment by one which has the result of this loop running `planePostion.Count / 2` times. – juharr Sep 25 '14 at 12:23
  • this code should work. But I think one of the following is your problem: 1. You determine whether you have a duplicate value *after* the `planePosition.RemoveAt(r);` At which point r no longer refers to the right value. 2. Your planePosition array contains duplicates to start with. – Rudolfwm Sep 25 '14 at 13:31

5 Answers5

0

Just do

readonly Random _random= new Random(); 


for( int a=0; a< planePosition.Count; a++)
{ 
int r= _random.Range(0,planePosition.Count);
Newplan.transform.position =  new Vector3 (planePosition[r].x, planePosition[r].y ,9.990011f);
planePosition.RemoveAt(r);
}
Jack M
  • 2,564
  • 4
  • 28
  • 49
0

There is no random function that guarantees that a value will not be repeated twice. That's why it is random. That means that your code will be fragile if you count on that.

Instead you can do mantain a collection to keep track of the used ID's

List<int> usedIndexes = new List<int>();

and then have a method to get a non-repeating index:

private int GetRandomInt(int min, int max)
{
     int num = Random.Range(min, max);

     while(usedIndexes.Contains(num))
     {
          num = Random.Range(min, max);
     }

     return num;
}
Faris Zacina
  • 14,056
  • 7
  • 62
  • 75
0

Because of my current reputation I am not able to comment. Please don't see that as answer.

I have some Questions:

1) See comment of juharr.

2) Here you are setting always (a other) the same Field/Property:

Newplan.transform.position =  new Vector3 (planePosition[r].x, planePosition[r].y ,9.990011f);

Is that your desired behaviour?

3) By removing the value that you used from the list, i think, you ensure that you will not use it again. That has nothing to do with random. This should also work if you always take the first value of the list. But: Are you sure that the values in your list are unique? Maybe your problem is not located in that method. instead it is the method that provides the planePosition-list.

Greenhorn
  • 74
  • 6
0

I got my answer :)

for(int a=0; a<16; a++)
        {


            //rnd = new Random ();
            while (planePosition.Count != 0)
            {
                     int index = Random.Range (0, planePosition.Count-1);
                     randomizedList.Add (planePosition [index]);
                    Newplan.transform.position =  new Vector3 (randomizedList[r].x, randomizedList[r].y ,9.990011f);
                    planePosition.RemoveAt (index);

            }

        }
kajal
  • 13
  • 3
0
  private void ScoreRandomize()
    {
        List<int> listRnd = new List<int>();
        listRnd.AddRange(scoreAmount); //scoreAmount is array or list with orginal data
        for (int i = 0; i < listRnd.Count + i; i++)
        {
            int rnd = UnityEngine.Random.Range(0, listRnd.Count);
            scoreDisplayTxts[i].text = listRnd[rnd].ToString();
            scoreAmount[i] = listRnd[rnd];
            listRnd.RemoveAt(rnd);
        }

    }
AFRA
  • 1