生成随机数组的问题

elysium1984 2008-12-01 02:18:16
需要一个方法,接收参数,然后生成随机数组。
举例:如果参数是 5
生成的数组就是把1 2 3 4 5 打乱顺序放在数组中。
那位大侠帮帮忙!
...全文
199 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
elysium1984 2008-12-01
  • 打赏
  • 举报
回复
谢谢楼上的程序,及其他各位的参与,分不多,代表心意!
yfcomeon 2008-12-01
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;

namespace Random
{
class Program
{
static void Main(string[] args)
{
RandomList randomList = new RandomList(8);
List<int> tt = randomList.Random();
foreach (int k in tt)
{
Console.WriteLine(k);
}
Console.ReadLine();
}


}

class RandomList
{
private List<int> _list;
private int _maxInt;

public RandomList(int maxInt)
{
_maxInt=maxInt;
_list = new List<int>(maxInt);
for (int i = 1; i <= maxInt;i++ )
{
_list.Add(i);
}
}

public List<int> Random()
{
List<int> tempList = new List<int>(_maxInt);


int temp;
for (int i = _maxInt; i > 0;i-- )
{
System.Random random = new System.Random();

temp = random.Next(0, i-1);
tempList.Add(_list[temp]);
_list.RemoveAt(temp);

}
return tempList;
}
}
}

elysium1984 2008-12-01
  • 打赏
  • 举报
回复
数组里面的数不能重复!
松花皮蛋 2008-12-01
  • 打赏
  • 举报
回复
Random randObj = new Random();
int start=1;//随机数可取该下界值
int end=5;//随机数不能取该上界值
int[] myarray = new int[end];
for( int j= start ; j < end; j++ )
{
myarray[randObj.Next( start, end)]=j;

}
dlmeijianyu 2008-12-01
  • 打赏
  • 举报
回复
up

110,571

社区成员

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

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

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