This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Here is my code, which has a workaround of resetting the Random; but this seems like it's more taxing to the system
namespace SloppyJoesRestaurant
{
internal class Chance
{
public static Random Randomizer = new Random();
//must reset the random to avoid seed problems:
private static void ResetRandomizer(int seed)
{
//Randomizer.state = 0;
Chance.Randomizer = new Random(Randomizer.Next(0, seed));
}
public static bool PercentChance(double chance)
{
ResetRandomizer(500);
//generate a random number to compare
double randomTwoDigit = Randomizer.NextDouble() * 100;
if(chance < randomTwoDigit)
{
return false;
}
return true;
}
public static bool Fifty()
{
ResetRandomizer(500);
return PercentChance(50);
}
public static string randomFromStringArray(string[] stringArray)
{
ResetRandomizer(500);
//Random random = new Random();
int randomIndex = Randomizer.Next(0,stringArray.Length-1);
//int randomIndex = random.Next(0,stringArray.Length-1);
return stringArray[randomIndex];
}
public static int RandomInt(int min, int max)
{
ResetRandomizer(max);
return Randomizer.Next(min, max);
}
}
}
if I don't use the ResetRandomizer, then I have the problem of identical seeding, and I get the same results if called multiple times in quick succession.
One other solution I can think of is to have the methods ask for a Random object as a parameter. But this seems sub-optimal to me too, I don't want to have to write Chance.PercentChance(50,myRandom) every time.
I'm quite new obviously, using Visual Studio and the Head First C# book.
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/csharp/comm...