C# Random Numbers aren't being “random”

后端 未结 5 1635
情歌与酒
情歌与酒 2020-11-28 15:40

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 theE         


        
相关标签:
5条回答
  • 2020-11-28 16:22

    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.

    0 讨论(0)
  • 2020-11-28 16:29

    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.

    0 讨论(0)
  • 2020-11-28 16:33

    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.

    0 讨论(0)
  • 2020-11-28 16:35

    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 讨论(0)
  • 2020-11-28 16:41

    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.

    0 讨论(0)
提交回复
热议问题