I've tried everything I know ( and that's really little ) and read everything I found, but I still don't know what's wrong here...

Short version... If I must write more details, please tell me

I have small program with only one class. All it has to do is to thrrow two dices.

So, I have class "Dice" with Value field and one method called Roll() which returns rnd1.Next(1,7). (rnd1 is declared in class earlier).

Now, I have two objects of that class. dice1 and dice2. When I call dice1.Value=dice1.Roll(); and dice2.Value=dice2.Roll();
It always give me two same numbers! Now I'm thinking to take my laptop to some casino or something. We can't lose...

Like I sais I've tried this on several more ways, but nothing. Please tell me what's wrong here

Recommended Answers

All 9 Replies

Most likely you are creating a new Random object each time you execute Roll() and since (if you don't specify) Random is seeded by the time, there hasn't been enough time between the creation of the objects for the time to have changed. This is a very common problem. If you post your Dice class, I can suggest some changes that will fix this for you.

I agree that the problem is probably your random initialisation. A quick and dirty solution would be to make your Random object in the Dice class (I assume that is rnd1?) a static variable. Then all instances of the dice class will share the random generator, meaning the numbers should be different.

Hope that helps ;)
M

Here's the code:

public class Dice
{
    public int Value;
    Random rnd1=new Random();

    public int Roll()
    {
        return rnd1.Next(1, 7);
    }
}

and

public partial class Form1 : Form
{
    Dice dice1;
    Dice dice2;
    public Form1()
    {
        InitializeComponent();
        dice1 = new Dice();
        dice2 = new Dice();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        dice1.Value = dice1.Roll();
        dice2.Value = dice2.Roll();
        label1.Text = dice1.Value.ToString();
        label2.Text = dice2.Value.ToString();
    }
}

I agree that the problem is probably your random initialisation. A quick and dirty solution would be to make your Random object in the Dice class (I assume that is rnd1?) a static variable. Then all instances of the dice class will share the random generator, meaning the numbers should be different.

Hope that helps ;)
M

And with

static Random rnd1=new Random();

it works perfectly :). TY!

Glad I could help. :) Do you understand why it works?

if you initialize both of them at the same time, the default time value will seed the same value to both. you could just make one random gen and use it to get both rolls.

I am very new in object oriented programming, so I still have trouble with basics about objects, classes, methods... Anyway, I'm good with mathematic and algorithm things, so, I guess it takes time to understand objects in a right way. It is very confusing for me to create object instance in one class, then create object or two of that class, and now think how inner object, which is now field, behaves. So, something I'm just trying to "swallow" for now, and get back to it later, as I become better and able to understand more complicate things.

If everthing here is about time why this doesn't works either:

dice1.Value = dice1.Roll();
System.Threading.Thread.Sleep(10);
dice2.Value = dice2.Roll();

I tried with Sleep(100) and Sleep(1000) too

The problem is not when you roll the dice, the problem is when you initialise. In general, there is no way to generate random numbers (maybe there is, but not as simple as this). The random class creates what is called a pseudo-random number generator. That means that it appears random to us, and is statistically random.

However, in the background is a function which is completely predictable.
Now these pseudo-random number generators need a 'seed', that is, a number which will be the input to the generating function. When you call new Random(), it uses the current computer time in ticks as the seed.

However, if you create 2 Randoms one after the other, there is a high chance that they will get the same seed, which means they will give you the same values.

Don't worry, it's a very common problem. You'll bang your head against the wall for the rest of your life with random numbers :) But it's worth it

I appriciate very much your help. Thanks to all! Thanks swinefish!

P.S.
If I ever make better Random class than it is, I'll let you know :-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.