一个面试题:生成0~100不重复的随机数

bancxc 2010-10-14 03:16:58
    class Program
{
static void Main(string[] args)
{
List<Node> list = new List<Node>();
Random r = new Random();
for (int i = 0; i < 100; i++)
{
list.Add(new Node{Index=i,Value=r.Next(0,100)});
}

list.Sort(delegate(Node a, Node b) { return a.Value - b.Value; });

foreach (Node node in list)
{
Console.WriteLine(node.Index);
}
Console.ReadKey();
}
}

class Node
{
public int Index
{
get;set;
}
public int Value
{
get;set;
}
}
这个可行不,谁有好点的办法
...全文
1474 43 打赏 收藏 转发到动态 举报
写回复
用AI写文章
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
shixiujin 2010-10-19
  • 打赏
  • 举报
回复
学习了...
jeansy911 2010-10-19
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20101014/10/a8c40934-022f-4141-b726-cca395e8b256.html?83258

/*
2层循环纯算法;
用list<>优化的2层循环纯算法;
用Arraylist,Contains,Add筛选的;
先生成随机数组,然后洗牌;
*/


//以及为了实现而实现的方法,机器不好的不要跑了,效率很高:


import java.util.Random;


public class test {
public static void main(String[] args) {
byte list[] = new byte[3000];

for(int i=0;i<list.length;i++){
list[i] = 0;
}

Random random = new Random();
int k = 0;

for(int j=0;j<1000;j++){
k = random.nextInt(2000);

if(list[k] == 1){
System.out.println(k+"已存在数组中");
}else{
System.out.println(k+"不在数组中,将它放入");
list[k] = 1;
}
}

System.out.println("数组的内容是:");
for(int l=0;l<list.length;l++){
if(list[l] == 1){
System.out.print(l+",");
}
}
}
}


精彩代码期待以后的牛人们实现。。
ITcql 2010-10-19
  • 打赏
  • 举报
回复
12楼的那位,确实不错。
hnelfh 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 shaofei830927 的回复:]
C# code


Random random = new Random();

LinkedList<int> lList=new LinkedList<int>();

for(i=1;i<=100;i++)
{
lList.add(i);
}

for(i=1;i<=100;i++)
{
int iRan = random .Next(0,l00-i);
……
[/Quote]
还不是个错的,!
wanghui0380 2010-10-19
  • 打赏
  • 举报
回复
只需要 list.Sort() 改动它就成,让他不根据A,b本身返回,而是直接随机返回-1,0,1这个3态结果即可

就像老p说滴,只是重新洗牌而已
yhcctv 2010-10-19
  • 打赏
  • 举报
回复
从概率上看,将一个数组随机打散获得的随机数跟随机生成的随机数差不多吧
无涯大者 2010-10-19
  • 打赏
  • 举报
回复
class Program
{
static void Main(string[] args)
{
int[] array = new int[100]; //定义一个长度为100数组
for (int i = 0; i < array.Length; i++)
{
label://标签
Random Rnd = new Random();
int a = (int)Rnd.Next(1, 101);
array[i] = a; //产生随机数赋值给数组
for (int j = 0; j < i; j++)
{
//判断随机数是否有重复
if (array[j] == array[i])
{
goto label; //跳转执行到label,继续产生随机数
}
}
Console.Write(array[i] + " \t");//输出数组中的元素
}
}
}
加油馒头 2010-10-19
  • 打赏
  • 举报
回复
楼主的 算法 绝对会有重复值

每次生成 0-100 的随机数,最后排序,你能确保 生成的随机数没有重复的?



楼上除了12楼方法正确而且效率高些,其他的要么 效率低,要么会有重复的
badyue 2010-10-19
  • 打赏
  • 举报
回复
int[] data = new int[101];
for (int i = 0; i <= 100; )
{
data[i] = i++;
}

Random rand = new Random();
for (int i = 0; i <= 100; i++)
{
int r = rand.Next(0, 101);
int v = data[r];
data[r] = data[i];
data[i] = v;
}

for (int i = 0; i <= 100; i++)
{
System.Out.Print(data[i]);
}
  • 打赏
  • 举报
回复
嗯,我刚看到 #12 已经写出来了。

不过我倒是不反对考什么算法时使用任何工具。使用不同的工具,我们正好可以进一步问问使用者是否熟悉工具的原理,以免花大价钱找来一个只知道自己重新研究和重新发明最低级的底层工具的学者而非技工。
  • 打赏
  • 举报
回复
这叫“生成”随机数?

这是重新洗牌而已。产生所有的数,然后重复多次:使用随机数作为选择指标把所选择的数与第一个数对调位置。
silenceHS 2010-10-19
  • 打赏
  • 举报
回复
学习 学习····
dalmeeme 2010-10-18
  • 打赏
  • 举报
回复
用数组的话:
		int[] numbers = new int[100];
for (int i = 0; i < 100; i++)
numbers[i] = i;
Random rd = new Random();
int index, realLength = numbers.Length, temp;
for (int i = 0; i < 100; i++)
{
index = rd.Next(realLength);
Response.Write(numbers[index] + "<br/>");
temp = numbers[index];
numbers[index] = numbers[realLength - 1];
numbers[realLength - 1] = temp;
realLength--;
}
dalmeeme 2010-10-18
  • 打赏
  • 举报
回复
		ArrayList numbers = new ArrayList();
for (int i = 0; i < 100; i++)
numbers.Add(i);
Random rd = new Random();
int index;
for (int i = 0; i < 100; i++)
{
index = rd.Next(numbers.Count);
Response.Write(numbers[index] + "<br/>");
numbers.RemoveAt(index);
}
JK0803Tangxu 2010-10-18
  • 打赏
  • 举报
回复
public static void RandOutput(int[] data,ref int flag)//data数组用于记录己经产生的随机数,flag用于标记当前data数组中最后一个元素的下标
{
Rand rand=new Rand();//创建一个随机数类的实例
while(1)
{
int i=rand.next(0,100);//产生一个0到100的随机数
while(1)
{
for(int j=0;j<=d;j++)
{
if(i!=d)//产生了不重复的随机数,函数返回
{
Console.WriteLine("产生的随机数为:{0}",i);
data[++flag]=i;
return;
}
i=rand.next(0,100);//没有产生不重复的随机数,继续产生随机数
}
}
}
0王涛0 2010-10-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
Random rand = new Random(Guid.NewGuid().GetHashCode());
rand .Next(最小值,最大值)
Random rnd = new Random(DateTime.Now.Millisecond);

Enumerable.Range(1, 100).OrderBy(Guid.NewGuid());
[/Quote]
支持
kingdizzy 2010-10-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hujinn 的回复:]

C# code

List<int> NumList = new List<int>();
Random rand = new Random();
while (NumList.Count < 100)
{
int Num = rand.Next(1, 1000);
……
[/Quote]
UP
黄_瓜 2010-10-18
  • 打赏
  • 举报
回复
shaofei830927 2010-10-15
  • 打赏
  • 举报
回复


Random random = new Random();

LinkedList<int> lList=new LinkedList<int>();

for(i=1;i<=100;i++)
{
lList.add(i);
}

for(i=1;i<=100;i++)
{
int iRan = random .Next(0,l00-i);

// 取随机数
lList[iRan];

lList.RemoveAt(iRan);
}





不好意思 写错了
shaofei830927 2010-10-15
  • 打赏
  • 举报
回复

Random random = new Random();

LinkedList<int> lList=new LinkedList<int>();

for(i=1;i<=100;i++)
{
lList.add(i);
}

for(i=1;i<=100;i++)
{
int iRan = random .Next(0,l00-i);

// 取随机数
lList[iRan];
}



随便写的就是这个意思 没有测试
加载更多回复(17)

110,499

社区成员

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

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

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