C# 如何产生五个随机的数字??

junsanrao 2008-08-07 11:22:01
System.Random r = new Random();
int yourandom = r.Next(01,46);

这个方法可以产生一个,如何去一次性产生5个呢?在一个按纽下,点下这个按纽就产生5个数字
如何产生打个比方01 05 46 21 08这样
...全文
825 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
junsanrao 2008-08-07
  • 打赏
  • 举报
回复
各位分不多,挑好数字给,见量
junsanrao 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 mohugomohu 的回复:]
如何产生打个比方01 05 46 21 08这样
ToString("00")
[/Quote]
你太帅了,谢谢
junsanrao 2008-08-07
  • 打赏
  • 举报
回复
ok 谢谢各位,但是要怎么输出
01 02 03 02 05这些数字前面的0呢?
整形不能这样输出
mohugomohu 2008-08-07
  • 打赏
  • 举报
回复
如何产生打个比方01 05 46 21 08这样
ToString("00")
vshirleyzhxl 2008-08-07
  • 打赏
  • 举报
回复
using System.Collections;

/// <summary>
/// 产生随机数
/// </summary>
/// <param name="len">产生随机数的个数</param>
/// <param name="min">随机数的下界</param>
/// <param name="max">随机数的上界</param>
/// <returns></returns>
private ArrayList getRadom(int len,int min,int max)
{
Random r = new Random();
ArrayList al = new ArrayList();
for (int i = 0; i < len; i++)
{
al.Add(r.Next(min, max));
}
return al;
}

调用
ArrayList al = new ArrayList();
al = getRadom(5, 0, 100);
foreach (int a in al)
{
MessageBox.Show(a.ToString());
}
davidxu1969 2008-08-07
  • 打赏
  • 举报
回复
楼主是要得到5个不重复的值吧?

如果是,这样:

//1.声明一个数组,保存结果

int[] result = new int[5];

//2.建个集合保存等选的项
List<int> numbers = new List<int>();
for(int i=0;i<46;i++)
{
numbers.Add(i+1);
}

//3.循环5次,每次得到一个随机索引,
// 然后从待选集合里根据索引选出结果,放到结果数组中
// 从待选集合里移除这一项
for(int i=0;i<5;i++)
{
Random random = new random();
int index = random.Next(numbers.Count);
result[i]=numbers[index];
numbers.RemoveAt(index);
}

result里的5个数就是楼主想要的。
sxmonsy 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 nevana 的回复:]
System.Random r = new Random();
int[] arrayInt = new int[5];
for(int i=0;i <5;i++)
{
int yourandom = r.Next(01,46);
arrayInt[i] = yourandom ;
}

用的时候在写个循环读数组,呵呵
[/Quote]
sxmonsy 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 nevana 的回复:]
System.Random r = new Random();
int[] arrayInt = new int[5];
for(int i=0;i <5;i++)
{
int yourandom = r.Next(01,46);
arrayInt[i] = yourandom ;
}

用的时候在写个循环读数组,呵呵
[/Quote]
浪漫幕末 2008-08-07
  • 打赏
  • 举报
回复
需要引用名空间using System.Collections.Generic;

你自己也可以改写一些,方法嘛就是那样。
浪漫幕末 2008-08-07
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
List<string> a;
getRandomNo(unchecked((int)DateTime.Now.Ticks), 5, out a);
for (int i = 0; i < a.Count; i++)
Label1.Text +=","+ a[i];
}

public void getRandomNo(int seed, int num, out List<string> no)
{
List<string> list = new List<string>();
Random a = new Random(seed);
for (int i = 0; i < num; i++)
{
list.Add(a.Next(1, 99).ToString().PadLeft(2, '0'));
}
no = list;
}
cww2010 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wen_ye 的回复:]
用循环,就可以哈
[/Quote]
蝶恋花雨 2008-08-07
  • 打赏
  • 举报
回复
   #region//生验证码随即数字+字母组合
private string RndNum(int VcodeNum)
{
string Vchar = "a,b,c,d,e,0,1,2,3,4,5,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,0,1,2,3,4,5,6,7,8,9,w,x,y,z,a,b,c,d,e,f,g,h,i,j,k,l,m,1,2,3,4,5,6,7,8,9,n,o,p,q,r,s,t,u,v,w,x,y,z";
string[] VcArray = Vchar.Split(new Char[] { ',' });
string VNum = "";
int temp = -1;

Random rand = new Random();

for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}

int t = rand.Next(35);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
nevana 2008-08-07
  • 打赏
  • 举报
回复
System.Random r = new Random();
int[] arrayInt = new int[5];
for(int i=0;i<5;i++)
{
int yourandom = r.Next(01,46);
arrayInt[i] = yourandom ;
}

用的时候在写个循环读数组,呵呵
zhangqy2008 2008-08-07
  • 打赏
  • 举报
回复
一次只有一个,那就让他循环运行五次
LQknife 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wangxb8888 的回复:]
random
[/Quote]
ericzhangbo1982111 2008-08-07
  • 打赏
  • 举报
回复
Random r=new Random();
List<int> list=new List<int>();
for(int i=0;i<=5;i++)
list.Add(r.Next(1,50));
stning 2008-08-07
  • 打赏
  • 举报
回复
for(int i=0;i<=5;i++){
//你的随机数
}
onthebox 2008-08-07
  • 打赏
  • 举报
回复
循环五次……
(不要在循环体中做类的申请)
wen_ye 2008-08-07
  • 打赏
  • 举报
回复
用循环,就可以哈
wangxb8888 2008-08-07
  • 打赏
  • 举报
回复
random

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧