I have problem with shuffling array in several objects of the same class.
I use function void shuffle()
on constructor call.
After printing my dataCharset array it turns out that every object have the same shuffled array.
Im using .net framework 4.8 in console application.
I have tried using a temporary array and then copying it shuflled way into dataCharset array ( which is my targed array i need to shuffle ).
char[] dataCharset =
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'!', '$', '#', '@', '-'
};
void shuffle()
{
Random random = new Random();
for (int i = 0; i < dataLength; i++)
{
int index = random.Next(0, dataLength);
char temp = dataCharset[index];
dataCharset[index] = dataCharset[i];
dataCharset[i] = temp;
}
}
Constructor looks like:
public Guesser()
{
dataLength = dataCharset.Length;
shuffle();
Console.WriteLine(dataCharset);
}
And my main file where i am creating object
Guesser is my class
Guesser guesser1 = new Guesser();
Guesser guesser2 = new Guesser();
Guesser guesser3 = new Guesser();
Guesser guesser4 = new Guesser();
Why every object have the same array if i am creating it with 'new' keyword? I expected that each object will have its own shuffled array.