topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 1:51 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: c# assignment: Problem with random  (Read 7851 times)

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
    • View Profile
    • Mediaguy
    • Donate to Member
c# assignment: Problem with random
« on: March 19, 2008, 05:21 PM »
I'm trying to pick two random numbers for assignment 5 of the c# school. I'm using the following code

Code: C# [Select]
  1. private void fillFirstBoxes()
  2.         {
  3.             userCard1Text.Text = Convert.ToString(functions.GetRandomCard());
  4.             dealerCard1Text.Text = Convert.ToString(functions.GetRandomCard());
  5.         }
  6.  
  7. ...
  8.  
  9.     public class Functions
  10.     {
  11.         private int RandomNumber(int min, int max)
  12.         {
  13.             Random random = new Random();
  14.             return random.Next(min, max);
  15.         }
  16.  
  17.         public int GetRandomCard()
  18.         {
  19.             int[] cards = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
  20.             int random = RandomNumber(0, 12);
  21.             return cards[random];
  22.         }
  23.     }

The trouble is that 90% of the time the two numbers are the same. Occasionally (about 10% of the time) the numbers are different but it is rare. So what am I doing wrong?

I know that computer random isn't really random, but surely it only takes a tick?
Learning C# - Graham Robinson

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: c# assignment: Problem with random
« Reply #1 on: March 19, 2008, 10:40 PM »
i'm not much of a c# person but something must be wrong.  do you need to randomize the random number generator?

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
    • View Profile
    • Donate to Member
Re: c# assignment: Problem with random
« Reply #2 on: March 20, 2008, 09:32 AM »
You're creating a new Random number generator each time you call RandomNumber() - you want to use the same generator for the whole random sequence.  I can't test this out right now because I'm traveling, but if you change the RandomNumber() method to something like:


Code: C# [Select]
  1. private int RandomNumber(int min, int max)
  2. {
  3.     static Random random = new Random();
  4.     return random.Next(min, max);
  5. }

You'll get a single instance of the Random generator that gets used for the entire random number sequence.


rht341

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 8
    • View Profile
    • Donate to Member
Re: c# assignment: Problem with random
« Reply #3 on: March 20, 2008, 10:59 AM »
Hello,

You need to supply the Random constructor with a seed value.  See http://msdn2.microso...ibrary/ctssatww.aspx for examples.

I'm trying to pick two random numbers for assignment 5 of the c# school. I'm using the following code

Code: C# [Select]
  1. private void fillFirstBoxes()
  2.         {
  3.             userCard1Text.Text = Convert.ToString(functions.GetRandomCard());
  4.             dealerCard1Text.Text = Convert.ToString(functions.GetRandomCard());
  5.         }
  6.  
  7. ...
  8.  
  9.     public class Functions
  10.     {
  11.         private int RandomNumber(int min, int max)
  12.         {
  13.             Random random = new Random();
  14.             return random.Next(min, max);
  15.         }
  16.  
  17.         public int GetRandomCard()
  18.         {
  19.             int[] cards = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
  20.             int random = RandomNumber(0, 12);
  21.             return cards[random];
  22.         }
  23.     }

The trouble is that 90% of the time the two numbers are the same. Occasionally (about 10% of the time) the numbers are different but it is rare. So what am I doing wrong?

I know that computer random isn't really random, but surely it only takes a tick?

-mediaguycouk (March 19, 2008, 05:21 PM)

mwb1100

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,645
    • View Profile
    • Donate to Member
Re: c# assignment: Problem with random
« Reply #4 on: March 20, 2008, 12:09 PM »
The trouble is that 90% of the time the two numbers are the same. Occasionally (about 10% of the time) the numbers are different but it is rare. So what am I doing wrong?
-mediaguycouk (March 19, 2008, 05:21 PM)

What's happening is that each time you call "new Random()" you are getting a new sequence of random numbers that are seeded by whatever the .NET framework uses by default (probably a timer).  Since you're getting the new random sequences one after the other very quickly, you're getting multiple random number sequences that have the same seed, and therefore have the same sequence of numbers (and you're only getting the first number from that sequence each time).

If you use just a single instance of the Random class, it will be seeded a single time (by default using the timer or whatever) and each time the Random.Next() method is called on that instance you'll get the next random number in the sequence.

I think that using the default seed is appropriate in this case, so you don't need to mess with seeding the generator (you're already doing that whether you know it or not) - you just need to seed the generator once (which happens in the "new Random()" call) and use that generator instead of creating a new generator each time.

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
    • View Profile
    • Mediaguy
    • Donate to Member
Re: c# assignment: Problem with random
« Reply #5 on: March 20, 2008, 12:23 PM »
So

Code: C# [Select]
  1. public class Functions
  2. {
  3.        private int RandomNumber(int min, int max)
  4.        {
  5.            Random random = new Random();
  6.            return random.Next(min, max);
  7.        }
  8. }

Becomes

Code: C# [Select]
  1. public class Functions
  2. {
  3.        Random random = new Random();
  4.  
  5.        private int RandomNumber(int min, int max)
  6.        {
  7.            return random.Next(min, max);
  8.        }
  9. }

?

Thanks, I'll try it out
Learning C# - Graham Robinson

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: c# assignment: Problem with random
« Reply #6 on: March 20, 2008, 03:13 PM »
Actually, this is what I'd do (Testing it is up to the user... ;))

    class Assignment1
    {
        private Random RandomNumber = new Random();
        private int[] CardList = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };

        public int GetRandomCard()
        {
            return CardList[RandomNumber.Next(0, 12)];
        }
    }
« Last Edit: March 20, 2008, 04:18 PM by wraith808 »

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 247
    • View Profile
    • Mediaguy
    • Donate to Member
Re: c# assignment: Problem with random
« Reply #7 on: March 20, 2008, 06:48 PM »
This is why I'm still on the 'c# 2005 for dummies' book.

But at least the application works :)

Learning C# - Graham Robinson