Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dice generator thing

Started by Vod Aug 21, 2007 at 12:18 PM 3 replies 1.5k views
Original Post
Vod
Vod
Can someone please point out where I'm going wrong with this code & how to fix it:

            int totalDice = 0;
            
            for (int i = 0; i < 3; i++)
            {
                Random dice = new Random();
                int RandomNumber = dice.Next(1, 6);
                totalDice += dice; //compiler doesn't like this
            }

All I want is a dice that can be thrown a number of times & the results added up to give a final total. This is doing my head in -every time I go to add some new thing to my project I end up wasting 3 - 4 hours banging my head against a simple little brick wall like this. I'm beginning to wonder if I've actually learned anything at all these last few weeks.
SiCrane
SiCrane
totalDice += RandomNumber;
Mike.Popoloski
Mike.Popoloski
First of all, the compiler error is occurring because you are trying to add a Random object to and Int object. Instead of totalDice += dice, you need totalDice += RandomNumber.

Secondly, I would create the Random object outside of the loop. Everytime you recreate the random object, you reset the seed used to generate the random number. It may not make a difference here, but it is best if you create the Random object once and use it for all the random numbers you need.

EDIT: Doh! Too slow.
Mike Popoloski | Journal | SlimDX
Vod
Vod
lol thanks

(think I need to find a new hobby)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.