Jump to content
  • Advertisement
Sign in to follow this  
CKolding

Random numbers in C# [Solved]

This topic is 4167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am very new to C# and programming in general. I am trying to do a very simple combat simulator using pseudo-random numbers from the Random()-class. So far I have written a little main function which can only display the attack and defense outputs from the two classes Enemy and Character. The main-function isn't relevant here, so I didn't include it. My problem is that whenever I make an instance of the classes and make them perform the Attack and Defend methods, it is as if they all used the same random numbers and not their own instance of the random-class. If there are two ranges of random numbers, say attacks are from 3 to 9 and defenses are from 2 to 8, the pairs are identic. Any ideas of what I might be doing wrong? The code for the two classes looks like this:
public class Character
{
    public double attack = 40;
    public int attackDieMin = 3;
    public int attackDieMax = 9;
    public double defense = 40;
    public int defenseDieMin = 2;
    public int defenseDieMax = 8;
    public double hitpoints = 200;

    public double Attack(double attack)
    {
        Random attackDie = new Random();
        double attackdamage = attack * attackDie.Next(attackDieMin, attackDieMax);
        return attackdamage;
    }
    public double Defend(double defense)
    {
        Random defenseDie = new Random();
        double damagereduction = defense * defenseDie.Next(defenseDieMin, defenseDieMax);
        return damagereduction;
    }
}
public class Enemy
{
    public double attack = 40;
    public int attackDieMin = 3;
    public int attackDieMax = 9;
    public double defense = 40;
    public int defenseDieMin = 2;
    public int defenseDieMax = 8;
    public double hitpoints = 200;
    
    public double Attack(double attack)
    {
        Random attackDie = new Random();
        double attackdamage = attack * attackDie.Next(attackDieMin, attackDieMax);
        return attackdamage;
    }
    public double Defend(double defense)
    {
        
        Random defenseDie = new Random();
        double damagereduction = defense * defenseDie.Next(defenseDieMin, defenseDieMax);
        return damagereduction;
    }
}

[Edited by - CKolding on July 23, 2007 7:55:53 AM]

Share this post


Link to post
Share on other sites
Advertisement
You are instantiating the Random class many times, which will probably give you the same set of seeds. You have two options:

1) Declare one instance of Random somewhere and make it static. Then, have all other classes reference that one instance, so that all the numbers will be different.

2) Keep it as you have it now, but pass a different seed into each different Random object. A common practice is to pass the current system time, which will always be different for each Random you create.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!