Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

0
I want to make a class using Random, but I'm not sure how to use the same Random object for all of them.
Post Flair (click to view more posts with a particular flair)
Post Body

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.

Author
Account Strength
100%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
647,654
Link Karma
110,504
Comment Karma
533,903
Profile updated: 1 day ago
Posts updated: 5 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago