mardi 6 janvier 2015

"Reset" random in class

So the task was to make a program that selects three different chest pieces (color + piece) using Enums. I made one instance of a Random object but of course this gives the same number throughout the execution of the code since I only initialized one int holding the random number.


Now my question is, is there any way you can reset it (update/modify the seed) or something like this during the execution so I don't have to create x amounts of int (in my case) to hold the random numbers?


For example: Every time my int chestpiece is used it would reinstate itself.


Is this possible or do I have to create several chesspiece int's (in my case) in order to fix this.



private static void Main()
{
int chesspiece = Rand.Next(1, sizeof (EnumChestPiece) + 1);
int color = Rand.Next(1, sizeof (EnumColor) + 1);

EnumChestPiece firstPiece = (EnumChestPiece) chesspiece;
EnumChestPiece secondPiece = (EnumChestPiece) chesspiece;
EnumChestPiece thirdPiece = (EnumChestPiece) chesspiece;

EnumColor firstColor = (EnumColor) color;
EnumColor secondColor = (EnumColor) color;
EnumColor thirdColor = (EnumColor) color;

Console.WriteLine("{0} {1}", firstColor, firstPiece);
Console.WriteLine("{0} {1}", secondColor, secondPiece);
Console.WriteLine("{0} {1}", thirdColor, thirdPiece);
}

private enum EnumChestPiece
{
Pawn,
Knight,
Bishop,
Rook,
King,
Queen
}

private enum EnumColor
{
Black,
White
}




Aucun commentaire:

Enregistrer un commentaire