Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6377719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:54:38+00:00 2026-05-25T01:54:38+00:00

I know that the C# Random class does not make true random numbers, but

  • 0

I know that the C# Random class does not make “true random” numbers, but I’m coming up with an issue with this code:

    public void autoAttack(enemy theEnemy)
    {
        //Gets the random number
        float damage = randomNumber((int)(strength * 1.5), (int)(strength * 2.5));

        //Reduces the damage by the enemy's armor
        damage *= (100 / (100 + theEnemy.armor));

        //Tells the user how much damage they did
        Console.WriteLine("You attack the enemy for {0} damage", (int)damage);

        //Deals the actual damage
        theEnemy.health -= (int)damage;

        //Tells the user how much health the enemy has left
        Console.WriteLine("The enemy has {0} health left", theEnemy.health);
    }

I then call the function here (I called it 5 times for the sake of checking if the numbers were random):

        if (thePlayer.input == "fight")
        {
            Console.WriteLine("you want to fight");
            thePlayer.autoAttack(enemy1);
            thePlayer.autoAttack(enemy1);
            thePlayer.autoAttack(enemy1);
        }

However, when I check the output, I get the exact same number for each 3 function calls. However, each time I run the program, I get a different number (which repeats 3 times) like this:

 You attack the enemy for 30 damage.
 The enemy has 70 health left.

 You attack the enemy for 30 damage.
 The enemy has 40 health left.

 You attack the enemy for 30 damage.
 The enemy has 10 health left.

I will then rebuild/debug/run the program again, and get a different number instead of 30, but it will repeat all 3 times.

My question is: how can I make sure to get a different random number each time I call this function? I am just getting the same “random” number over and over again.

Here is the random class call that I used:

    private int randomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:54:38+00:00Added an answer on May 25, 2026 at 1:54 am

    My guess is that randomNumber creates a new instance of Random each time… which in turn creates a new pseudo-random number generator based on the current time… which doesn’t change as often as you might think.

    Don’t do that. Use the same instance of Random repeatedly… but don’t “fix” it by creating a static Random variable. That won’t work well either in the long term, as Random isn’t thread-safe. It will all look fine in testing, then you’ll mysteriously get all zeroes back after you happen to get unlucky with concurrency 🙁

    Fortunately it’s not too hard to get something working using thread-locals, particularly if you’re on .NET 4. You end up with a new instance of Random per thread.

    I’ve written an article on this very topic which you may find useful, including this code:

    using System;
    using System.Threading;
    
    public static class RandomProvider
    {    
        private static int seed = Environment.TickCount;
    
        private static ThreadLocal<Random> randomWrapper = new ThreadLocal<Random>
            (() => new Random(Interlocked.Increment(ref seed)));
    
        public static Random GetThreadRandom()
        {
            return randomWrapper.Value;
        }
    }
    

    If you change your new Random() call to RandomProvider.GetThreadRandom() that will probably do everything you need (again, assuming .NET 4). That doesn’t address testability, but one step at a time…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate random numbers manually. I know that every language have the
Excuse my emacs newbiness here, but does anybody know how to get around this?
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the following is true int i = 17; //binary 10001 int
I know that IList is the interface and List is the concrete type but
I am trying to reproduce something that System.Xml.Serialization already does, but for a different
I know, I know, now I have two problems 'n all that, but regex
I have a legacy function that looks like this: int Random() const { return
I know that it is not a good practice to call a method in
In this question of mine , @DeadMG says that reinitializing a class through the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.