11

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);
    }
Mento
  • 113
  • 1
  • 5

5 Answers5

37

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...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
10

You didn't show us the code for randomNumber. If it looks anything like

private int randomNumber(int m, int n) {
    Random rg = new Random();
    int y = rg.Next();
    int z = // some calculations using m and n
    return z;
}

Well, then there is your issue. If you keep creating new instances of Random, it's possible that they will sometimes have the same seed (the default seed is the system clock which has limited precision; create them quickly enough and they get the same seed) and then the sequence produced by this generator will always be the same.

To fix this, you have to instantiate an instance of Random once:

private readonly Random rg = new Random();
private int randomNumber(int m, int n) {
    int y = this.rg.Next();
    int z = // some calculations using m and n
    return z;
}

And to clear up another point, even if you do this, the output from Random is still not "true" random. It's only psuedorandom.

jason
  • 236,483
  • 35
  • 423
  • 525
  • 1
    +1 for the example. However, it's more likely that it will *almost always* yield the same values if invoked in very short succession.... ;-) "The default seed value is derived from the system clock and has finite resolution. " –  Aug 31 '11 at 01:42
  • @pst, no, it don't yield same value for that. The `seed` (initial value) depends on clock, but its internal state changes when you use it. – J-16 SDiZ Aug 31 '11 at 02:26
  • @J-16 SDiZ: I think what pst meant is that if you have a method that is instantiating a new instance of `Random` and returning a single random value, and invoke that method continuously in a loop, you will see the same value repeated a few times as the seed doesn't change, and then another value repeated a few times as the seed has changed from the clock changing, and so on. – jason Aug 31 '11 at 18:14
  • @jason, that is exactly what happened to me. I was so puzzled but the comments explain it perfectly. I was getting 2-3 repeats and it didn't look random. – nanonerd Aug 22 '16 at 00:16
0

Instantiate the object random outside the method. ( Random random = new Random(); should be written before the method)

It is also vital that you understand that random isn't really random.

Derek Brown
  • 4,232
  • 4
  • 27
  • 44
0

What is randomNumber?

Typically a pseudo-random number generator is seeded (with a time-related thing, or something random like a time between two keypresses or network packets or something).

You don't indicate what generator you are using, nor how it is seeded.

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
-2

if you generate random numbers in loop it will probably wont be random. because random numbers are basically created internally on current system time. So place this code in the loop:

Thread.Sleep(10);

So the system will go to sleep for 10 m sec. And you will get new fresh random number. Its a guaranteed solution. But this will also effect to performance of the system.

Yasin
  • 1,150
  • 5
  • 19
  • 39
  • During my confusion, using Sleep was my crude workaround when I kept getting 2-3 repeated values and was baffled. BTW, I was still getting repeats at (100). At (500), it looked random. But yeah, performance is obv awful. @Jason's answer above is a good fix. – nanonerd Aug 22 '16 at 00:19
  • It's not a good fix, it's absolutely terrible. Just use the Random class properly. – Thorham Jul 20 '22 at 06:15