日期:2014-05-20 浏览次数:21046 次
namespace Skyiv
{
using System.Collections.Generic;
class Random : System.Random
{
private List<int> int32List = new List<int>();
public override int Next(int minValue, int maxValue)
{
if (minValue == maxValue) return minValue;
int next = base.Next(minValue, maxValue);
if (int32List.Count >= maxValue - minValue) return next;
if (int32List.Contains(next)) return Next(minValue, maxValue);
int32List.Add(next);
return next;
}
}
}
namespace Test
{
using System;
using Rand = Skyiv.Random;
class Program
{
static void Main()
{
Rand r = new Rand();
Console.WriteLine(r.Next(21,25));
Console.WriteLine(r.Next(21,25));
Console.WriteLine(r.Next(21,25));
Console.WriteLine(r.Next(21,25));
Console.WriteLine(r.Next(21,25));
}
}
}
------解决方案--------------------
学习,收藏
------解决方案--------------------
@_@~
------解决方案--------------------
1: 把你最终需要的结果(不重复的数)预先放在一个数组中, 因为rand函数产生的随机数会重复,我们不直接用,而是用来对应数组的下标
2: rand产生一个随机下标,我们就取出对应数组中的值(我们真正需要的随机数)
3: 然后用数组最后一个值来替换该下标数组中的值
4: 将产生随机下标的范围减少一
5: goto 2
注: 3中所谓数组最后一个值是指产生随机下标范围内的最后一个.
如产生随机下标0-9, 第一次就用array[9]替换,第二次就用array[8]替换.
#include<time.h>
#include<stdlib.h>
#include <stdio.h>
#define NUM 10
int main()
{
int cont[NUM];
int index, i;
for (i=0; i<NUM; i++)
cont[i] = i;
srand((int)time(0));
for (i=0; i<NUM; i++) {
index = (int)((float)(NUM-i) * rand() / (RAND_MAX+1.0));
printf("%d ", cont[index]);
cont[index] = cont[NUM-1-i];
}
putchar('\n');
return 0;
}
------解决方案--------------------
路过,学习
------解决方案--------------------
^ō^ 先占一楼...
------解决方案--------------------
不懂C#,平时用EXCEL解决1~100的正整数随机排序时,使用下面的方法:
搞一个2维数组
第一维为随机数,第二维为等差序列
对该数组按照第一维排序,就得到第二维的随机顺序排列。