• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Tạo số ngẫu nhiên trong C# - Random Number in C#, Be careful of some of the samples you find

Admin

Well-Known Member
Staff member
Administrator
I recently came across this code on the internet for generating a random number with C#.

Code:
 private static int RandomNumber(int min, int max)
 {
    Random random = new Random();
    return random.Next(min, max);
 }
The problem with this code is that for a novice developer (and maybe even some advance developers) it can be misleading and in some instance will not work. Take for instance the following code…

The issue with this is not immediately obvious, but if you remember one thing about the Random object in C# is that when you do not pass it any parameters is uses a time based seed. In addition, if you use the same seed for an new instance the same numbers will be generated, thus they number really are not random. The same is also true for the Random.Next function, if called within the same time the same number as previous will be generated. So in the case of the function above, the for loop can go through the loop 4-5 times within a millisecond, so that means the same “random” number will be generated. So this function really does not generate a real random ID (since the same characters are repeated 4-5 times).

Code:
 public static string GenerateID(int MinSize, int MaxSize)

 {

    string stRefID = String.Empty;

    int iChosenMaxSize = RandomNumber(MinSize, MaxSize);

    for (int x = 1; x <= iChosenMaxSize; x++)

    {

       int iCharType = RandomNumber(1, 3);


       switch (iCharType) 

       {

          case 1:

          {

             stRefID += char.ConvertFromUtf32(RandomNumber(48, 57));

             break;

          }

          case 2:

          {

             stRefID += char.ConvertFromUtf32(RandomNumber(65, 90));

              break;

          }

          case 3:

          {

             stRefID += char.ConvertFromUtf32(RandomNumber(97, 122));

             break;

          }

       }

    }

    return stRefID; 

 }
So the code that does what is really needed cannot use the RandomNumber function that I found on the internet. Below is how the the function above can be written not using the RandomNumber function to do what is expected.

Code:
 public static string GenerateID(int MinSize, int MaxSize)

 {

    string stRefID = String.Empty;

    Random random = new Random();

    int iChosenMaxSize = random.Next(MinSize, MaxSize); 


    for (int x = 1; x <= iChosenMaxSize; x++)

    {

       int iCharType = random.Next(1, 3);


       switch (iCharType)

       {

          case 1:

          {

             stRefID += char.ConvertFromUtf32(random.Next(48, 57));

             break;

          }

          case 2:

          {

             stRefID += char.ConvertFromUtf32(random.Next(65, 90));

             break;

          }

          case 3:

          {

             stRefID += char.ConvertFromUtf32(random.Next(97, 122));

            break;

          }

       }

  

       System.Threading.Thread.Sleep(1);

    }
    return stRefID; 

    }


The 2 keys to the update function are:
1) The Random() object is only instantiated once so the seed is only set once.
2) The addition of the Thread.Sleep(1) call between each of the time through the loop. This makes sure that when the Next function is called the time has changed so that the number generated will not be the same as previously generated.
This is not a knock on the sample code I found, since it would work in certain instances. But just a warning to be aware that when downloading code samples (even if they get good reviews), might not work in the situation that you need it for and IMO need to be tested even more than code you wrote yourself.
 

Facebook Comments

Similar threads

New posts New threads New resources

Back
Top