C#随机数不是“随机"的. [英] C# Random Numbers aren't being "random"

查看:507
本文介绍了C#随机数不是“随机"的.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道C#随机类不会产生真正的随机"数字,但是我遇到了以下代码问题:

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

然后我在这里调用该函数(为了检查数字是否随机,我调用了5次):

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

但是,当我检查输出时,每3个函数调用得到的编号完全相同.但是,每次我运行该程序时,都会得到一个不同的数字(重复3次),如下所示:

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.

然后我将再次重建/调试/运行程序,并得到一个不同的数字而不是30,但是它将重复全部3次.

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

推荐答案

我的猜测是randomNumber创建了

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.

不要那样做.重复使用Random的相同实例...但是不要通过创建静态Random变量来修复"它.从长远来看,这也不行,因为Random并不是线程安全的.在测试中一切都会看起来不错,然后在碰巧并发性很不幸之后,您会神秘地将所有零归还:(

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 :(

幸运的是,使用线程本地方法进行工作并不难,尤其是在使用.NET 4的情况下.最终,每个线程都有一个Random的新实例.

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

如果将new Random()调用更改为RandomProvider.GetThreadRandom(),则可能会完成您需要的所有操作(同样,假设使用.NET 4).那不是解决可测试性,而是一次一步...

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

这篇关于C#随机数不是“随机"的.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆