开发者

Random.Next is not giving a random number

开发者 https://www.devze.com 2023-02-20 13:54 出处:网络
Hi This how I used the random but it always gives \"1\" index to indexOfAChosenListCell. When I debug it it shows different values, but on regular running I get the same move every time..

Hi

This how I used the random but it always gives "1" index to indexOfAChosenListCell.

When I debug it it shows different values, but on regular running I get the same move every time..

What is the problem with Random , it's static not random... :)

internal Square getAutomaticMove()                      
{
            List开发者_如何学Go<Square> LegalMovesArray = GetLegalSquares();
            Random randomListCell = new Random();
            int indexOfAChosenListCell = 0;

            if (CheckForLegalSquares())    
            {
                 indexOfAChosenListCell = randomListCell.Next(LegalMovesArray.Count-1);
            }


make randomListCell an instance variable and only initialize it once - otherwise you will keep getting the same numbers again.

From MSDN:

By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers.


Random.Next(Int32)

Returns a nonnegative random number less than the specified maximum

So the largest number you'll ever get is Count - 2, probably not what you wanted.


I would suggest seeding the random numer generator too. As I understand, you will end up with the same psuedo-random number sequence every time you run the application. I may be mistaken.

int Seed = (int)DateTime.Now.Ticks;
Random randomListCell = new Random(Seed);


Declare Random as a private member variable, then instantiate it the constructor, and only call Random.Next in your method.


Your random should be create outside the function

Random randomListCell = new Random();

internal Square getAutomaticMove()                      
{
            List<Square> LegalMovesArray = GetLegalSquares();
            int indexOfAChosenListCell = 0;

            if (CheckForLegalSquares())    
            {
                 indexOfAChosenListCell = randomListCell.Next(LegalMovesArray.Count-1);
            }


Don't create a new Random every time you need a new number; if you want a sequence of different random numbers, you want to keep a single Random object and ask it repeatedly for new ones.


I'll show the difference:

var random = new Random();
var color = Color.FromArgb(200, random.Next(255), // 222
                                random.Next(255), // 33
                                random.Next(255)); // 147

Result: #DE2193

var color = Color.FromArgb(200, new Random().Next(255), // 153
                                new Random().Next(255), // 153
                                new Random().Next(255)); // 153

Result: #999999


declare Random object outside the function, so that it use same object to generate new number. You are creating new object every time which use same seed and gives you mostly same numbers...

static Random randomListCell = new Random();
internal Square getAutomaticMove()                      
{
            List<Square> LegalMovesArray = GetLegalSquares();

            int indexOfAChosenListCell = 0;

            if (CheckForLegalSquares())    
            {
                 indexOfAChosenListCell = randomListCell.Next(LegalMovesArray.Count-1);
            }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号