C implements random number generation class instances
- 2020-12-26 05:51:56
- OfStack
This article is an example of C# implementing a random number generation class. Share to everybody for everybody reference. The specific analysis is as follows:
The main extension of this class is the use of random, which encapsulates a random number generation that is often used, can generate random numbers within a specified range, can randomly generate strings, etc
using System;
namespace DotNet.Utilities
{
/// <summary>
/// BaseRandom
/// Random number generation
///
/// Random number management, maximum and minimum values can be set by themselves.
/// </summary>
public class BaseRandom
{
public static int Minimum = 100000;
public static int Maximal = 999999;
public static int RandomLength = 6;
private static string RandomString = "0123456789ABCDEFGHIJKMLNOPQRSTUVWXYZ";
private static Random Random = new Random(DateTime.Now.Second);
#region public static string GetRandomString() Generate random characters
/// <summary>
/// Generate random characters
/// </summary>
/// <returns> string </returns>
public static string GetRandomString()
{
string returnValue = string.Empty;
for (int i = 0; i < RandomLength; i++)
{
int r = Random.Next(0, RandomString.Length - 1);
returnValue += RandomString[r];
}
return returnValue;
}
#endregion
#region public static int GetRandom()
/// <summary>
/// Random number generation
/// </summary>
/// <returns> The random number </returns>
public static int GetRandom()
{
return Random.Next(Minimum, Maximal);
}
#endregion
#region public static int GetRandom(int minimum, int maximal)
/// <summary>
/// Random number generation
/// </summary>
/// <param name="minimum"> The minimum value </param>
/// <param name="maximal"> The maximum </param>
/// <returns> The random number </returns>
public static int GetRandom(int minimum, int maximal)
{
return Random.Next(minimum, maximal);
}
#endregion
}
}
Hopefully this article has helped you with your C# programming.