C # Random numbers are not "random"

I know that the C # Random class does not do “true random” numbers, but I am facing a problem 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 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);
    }

Then I call the function here (I called it 5 times to check 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 number for every 3 function calls. However, every time I run the program, I get a different number (which is repeated 3 times):

 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.

Then I rebuild / debug / run the program again and get a different number instead of 30, but it will be repeated all three times.

My question is: how can I get different random numbers every time I call this function? I just get the exact same random number over and over again.

Here is a random call to the class that I used:

    private int randomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }
+5
5

, randomNumber Random... , , - , ..., , .

. Random... "" , Random. , Random . , , concurrency: (

, -, , .NET 4. Random .

, , :

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;
    }
}

new Random() RandomProvider.GetThreadRandom(), , , , ( , , .NET 4). , ...

+26

randomNumber.

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;
}

, . Random, , ( , , ), .

, Random :

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;
}

, , Random - "" . psuedorandom.

+7

randomNumber?

( - , , - ).

, , .

0

, , , . . :

Thread.Sleep(10);

, 10 . . . .

0

. (Random random = new Random(), )

It is also important to understand that random is not really random .

0
source

All Articles