C
C#2y 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
DarmaOP2y 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
DarmaOP2y 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
Angius2y ago
Well, rnd.Next(0, shuffled.Length) can generate repeating numbers Look into Fischer-Yates algorithm
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server