这里供给了两种要领,挪用其一即可:
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SJ_random
{
class Class_main
{
static void Main()
{
Class_random rdm = new Class_random();
Random rm = new Random();
string s = string.Empty; //置空字符串.
Console.WriteLine(rdm.random_1(s, rm)); //挪用要领 random_1 ,孕育产生随机数.
Console.Write(rdm.random_2(s, rm)); //挪用要领 random_2 ,孕育产生随机数.
Console.ReadKey();
}
}
public class Class_random
{
/// <summary>
/// 要领一,,生成随机数。
/// </summary>
/// <param name=”s”>接收生成的随机数</param>
/// <param name=”rm”>random的实例</param>
/// <returns>生成的随机数</returns>
public string random_1(string s, Random rm)
{
for (int i = 0; i < 4; i++)
{
int k = rm.Next();
char j = (char)(‘0’ + (char)(k % 10));
s += j.ToString();
}
return s;
}
/// <summary>
/// 要领二,生成随机数。
/// </summary>
/// <param name=”s”>接收生成的随机数</param>
/// <param name=”rm”>random的实例</param>
/// <returns>生成的随机数</returns>
public string random_2(string s, Random rm)
{
s = Convert.ToString(rm.Next(1000, 9999));