问题是:生成一个长度为10的字符窜,要求从26个字母中随机选取。
我的代码是:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace 字符串
{
class Program
{
private static int GetInt()
{
Random rm = new Random();
int x = rm.Next(0,27);
return x;
}
static void Main(string[] args)
{
ArrayList stral = new ArrayList();
string all = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 10; i++)
{
int x = GetInt();
string temp = all.Substring(x, 1);
stral.Add(temp);
}
foreach (string s in stral)
{
Console.Write(s);
}
Console.Read();
}
}
}
可是直接运行就会出现10个一样的字母,逐语句运行就正常,郁闷了 各位大侠指点下啊
我目前没分 希望大家帮个忙
我的代码是:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace 字符串
{
class Program
{
private static int GetInt()
{
Random rm = new Random();
int x = rm.Next(0,27);
return x;
}
static void Main(string[] args)
{
ArrayList stral = new ArrayList();
string all = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 10; i++)
{
int x = GetInt();
string temp = all.Substring(x, 1);
stral.Add(temp);
}
foreach (string s in stral)
{
Console.Write(s);
}
Console.Read();
}
}
}
可是直接运行就会出现10个一样的字母,逐语句运行就正常,郁闷了 各位大侠指点下啊
我目前没分 希望大家帮个忙
你可以试下放到类里面去定义~~
但是如果是直接运行,那么时间片段就很小了,所以随机数就一样了,解决的办法,如果是你那样也行,但是不推荐,有种好点的,就是你每次获得两个随机数,然后将这些随机数进行一些运算,可以获得不同的数,怎么算,我也不记得了,以前在网上看到过。