31

Possible Duplicate:
Access random item in list

I have an array with numbers and I want to get random elements from this array. For example: {0,1,4,6,8,2}. I want to select 6 and put this number in another array, and the new array will have the value {6,....}.

I use random.next(0, array.length), but this gives a random number of the length and I need the random array numbers.

for (int i = 0; i < caminohormiga.Length; i++ )
{
    if (caminohormiga[i] == 0)
    {
        continue;
    }

    for (int j = 0; j < caminohormiga.Length; j++)
    {
        if (caminohormiga[j] == caminohormiga[i] && i != j)
        {
            caminohormiga[j] = 0;
        }
    }
}

for (int i = 0; i < caminohormiga.Length; i++)
{
   int start2 = random.Next(0, caminohormiga.Length);
   Console.Write(start2);
}

return caminohormiga;
Community
  • 1
  • 1
Shebystian
  • 423
  • 1
  • 4
  • 6
  • 1
    With a simple random selection you will get duplicates. Do you want to return a shuffled copy of `caminohormiga` ? – H H Jan 12 '13 at 20:56
  • 1
    @shebystian are duplicates applicable?? – exexzian Jan 12 '13 at 20:57
  • Do you want to shuffle or to rotate your `caminohormiga` array? – Alexander Balte Jan 12 '13 at 21:01
  • If you dont want duplicates and assuming the output it would be better to use shuffle instead rather than checking for duplicates, which do takes time – exexzian Jan 12 '13 at 21:21
  • If you are ok with duplicates, view the accepted [answer](http://stackoverflow.com/a/14297876/444610). If you want it shuffled without duplicates, view this [answer](http://stackoverflow.com/a/14297930/444610). – Seth Flowers Feb 14 '17 at 17:09

6 Answers6

49

I use the random.next(0, array.length), but this give random number of the length and i need the random array numbers.

Use the return value from random.next(0, array.length) as index to get value from the array

 Random random = new Random();
 int start2 = random.Next(0, caminohormiga.Length);
 Console.Write(caminohormiga[start2]);
kame
  • 20,848
  • 33
  • 104
  • 159
Tilak
  • 30,108
  • 19
  • 83
  • 131
  • 1
    thanks! the code finally works, but the numbers are repeat, any idea to how avoid the repeat? – Shebystian Jan 12 '13 at 21:11
  • 1
    Yes, remove the element form array in the loop. – Tilak Jan 12 '13 at 21:14
  • To avoid the repeated numbers, use `IEnumerable.OrderBy(n => Guid.NewGuid())`. See my [answer](http://stackoverflow.com/a/14297930/444610). – Seth Flowers Feb 14 '17 at 17:06
  • 1
    Getting repeated items might be because of how `Random` class behaves. Try not to instantiate `Random` every you get a random number, keep it single instance. – SHM Jun 29 '21 at 07:35
30

To shuffle

int[] numbers = new [] {0, 1, 4, 6, 8, 2};
int[] shuffled = numbers.OrderBy(n => Guid.NewGuid()).ToArray();
Seth Flowers
  • 8,990
  • 2
  • 29
  • 42
5

You just need to use the random number as a reference to the array:

var arr1 = new[]{1,2,3,4,5,6}
var rndMember = arr1[random.Next(arr1.Length)];
faester
  • 14,886
  • 5
  • 45
  • 56
3

Try like this

int start2 = caminohormiga[ran.Next(0, caminohormiga.Length)];

instead of

int start2 = random.Next(0, caminohormiga.Length);
Murshid Ahmed
  • 946
  • 5
  • 20
3

I noticed in the comments you wanted no repeats, so you want the numbers to be 'shuffled' similar to a deck of cards.

I would use a List<> for the source items, grab them at random and push them to a Stack<> to create the deck of numbers.

Here is an example:

private static Stack<T> CreateShuffledDeck<T>(IEnumerable<T> values)
{
  var rand = new Random();

  var list = new List<T>(values);
  var stack = new Stack<T>();

  while(list.Count > 0)
  {
    // Get the next item at random.
    var index = rand.Next(0, list.Count);
    var item = list[index];

    // Remove the item from the list and push it to the top of the deck.
    list.RemoveAt(index);
    stack.Push(item);
  }

  return stack;
}

So then:

var numbers = new int[] {0, 1, 4, 6, 8, 2};
var deck = CreateShuffledDeck(numbers);

while(deck.Count > 0)
{
  var number = deck.Pop();
  Console.WriteLine(number.ToString());
}
Erik
  • 12,730
  • 5
  • 36
  • 42
  • Shouldn't rand.Next(0, list.Count); be rand.Next(0, list.Count - 1);? Because if we get for example index 5 and there is 5 elements we will get out of range exception i think. But i like your solution. – Hazaaa Nov 08 '21 at 13:19
0
Console.Write(caminohormiga[start2]);
SergeyS
  • 3,515
  • 18
  • 27