2

I have the following code. Random r works and gets me about 10% into the if. However rr doesn't seem to work. It always returns 0. What am I doing wrong?

I want to choose between two choices randomly only 10% of the time. This is in an asp.net app. The code gets executed on button click.

        Random r = new Random();
        Random rr = new Random();

        int randomnum = r.Next(0, 100);
        if (randomnum <= 10)
        {

            int randompick = rr.Next(0, 2);
            if (randompick == 0)
            {
Tigran
  • 872
  • 1
  • 9
  • 25
  • 7
    Couldn't you just use the same `Random` object for both random choices? – Sam Mussmann Aug 03 '12 at 23:21
  • 1
    **N.B.** _"The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers."_ http://msdn.microsoft.com/en-us/library/h343ddh9.aspx – Matt Ball Aug 03 '12 at 23:22
  • 1
    Good rule of thumb for the `Random` class: don't re-construct the RNG every time you need a number. Create one `Random`, store it in a variable with class scope, and just call `r.Next(X, Y)` whenever you need a number. – Dan J Aug 03 '12 at 23:24
  • You guys are great. Love it. Yes I don't know why I have two Random objects. @Sam you should put your comment as an answer because that's what I did and that's a very good answer. – Tigran Aug 03 '12 at 23:28
  • Fundamentally a duplicate of [Random number generator not working the way I had planned (C#)](http://stackoverflow.com/questions/767999/random-number-generator-not-working-the-way-i-had-planned-c) – Dan J Aug 03 '12 at 23:29

3 Answers3

4

If you are happy with the outer loop's randomness, consider

int randompick = randomnum % 2;

instead of the nested Random object.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
1

You could use the same Random object for both random choices, right?

Sam Mussmann
  • 5,883
  • 2
  • 29
  • 43
  • Sorry Sam now that I look at it Babak's answer is a little more elegant. But I do appreciate your answer very much. – Tigran Aug 03 '12 at 23:33
0

As noted, you should use just one pseudorandom stream and instantiate it just once. I'd struct my solution along these lines:

class SomeWidget
{
    private static Random rng ;

    static SomeWidget()
    {
        rng = new Random() ;
        return ;
    }

    public SomeWidget()
    {
        return ;
    }

    public int DoOneThing90PercentOfTheTimeAndSomethingElseTheRestOfTheTime()
    {
        int rc ;
        int n = rng.Next() % 10 ; // get a number in the range 0 - 9 inclusive.
        if ( n != 0  ) // compare to whatever value you wish: 0, 1, 2, 3, 4, 5, 6, 8 or 9. It makes no nevermind
        {
             rc = TheNinetyPercentSolution() ;
        }
        else
        {
            rc = TheTenPercentSolution() ;
        }
        return rc ;
    }

    private int TheTenPercentSolution()
    {
        int rc ;
        int n = rng.Next() % 2 ;
        if ( n == 0 )
        {
            rc = DoOneThing() ;
        }
        else
        {
            rc = DoAnotherThing() ;
        }
        return rc ;
    }

    private int DoOneThing()
    {
        return 1;
    }

    private int DoAnotherThing()
    {
        return 2 ;
    }

    private int TheNinetyPercentSolution()
    {
        return 3 ;
    }

}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135