C
C#16mo ago
Darma

❔ Shuffling Table Values

I'm creating a method that will take in the values of a table and then randomly shuffle them into different indexes of a new table using a for loop, how would I go about doing this?
5 Replies
Darma
Darma16mo ago
public static int[] shuffleArray(int[] array)
{
int[] shuffled = new int[array.Length];

for (int i = 0; i < shuffled.Length - 1; i++)
{

}

return shuffled;
}
public static int[] shuffleArray(int[] array)
{
int[] shuffled = new int[array.Length];

for (int i = 0; i < shuffled.Length - 1; i++)
{

}

return shuffled;
}
public static int[] shuffleArray(int[] array)
{
int[] shuffled = new int[array.Length];
Random rnd = new Random();

for (int i = 0; i < shuffled.Length; i++)
{
shuffled[i] = array[rnd.Next(0, shuffled.Length)];
Console.Write(shuffled[i] + ", ");
}

return shuffled;
}
public static int[] shuffleArray(int[] array)
{
int[] shuffled = new int[array.Length];
Random rnd = new Random();

for (int i = 0; i < shuffled.Length; i++)
{
shuffled[i] = array[rnd.Next(0, shuffled.Length)];
Console.Write(shuffled[i] + ", ");
}

return shuffled;
}
Darma
Darma16mo ago
I've made adjustments but the shuffled array has repeating numbers, and not every number from the input array is present in the newly shuffled array
Angius
Angius16mo ago
Well, rnd.Next(0, shuffled.Length) can generate repeating numbers Look into Fischer-Yates algorithm
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.