怎样获取随机数

kaiaiyaya 2011-06-29 09:31:08
我定义了一个数组:int[] num=new int[54]
从1-54中随机排列到这个数组中,每一个数都不相同,也就是说让1-54这54个数随机分配到数组中应该怎样实现呢??
最好能给一个方法 参数是个数 返回值是一个整形的数组 求高手指教!!在线等
...全文
513 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinwu7 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangwenquan123 的回复:]
C# code
int[] arr = new int[54];
List<int> list = new List<int>();
for (int i = 1; i <= 54; i++)
list.Add(i);
Random ran = new Random();
……
[/Quote]
比我想的要好,不献丑了!
kkyangx 2011-07-07
  • 打赏
  • 举报
回复
using System.Linq;

哇强人。。。
itliyi 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangwenquan123 的回复:]
C# code

int[] arr = new int[54];
List<int> list = new List<int>();
for (int i = 1; i <= 54; i++)
list.Add(i);
Random ran = new Random();
……
[/Quote]正解
肖恩 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 caozhy 的回复:]
给你一个简单而且易懂的代码,代码只要1行:


C# code

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

namespace ConsoleApplication1
{
class Program
{
st……
[/Quote]
这个是Linq???羡慕linq强人啊!!
jiangjinlinok 2011-07-07
  • 打赏
  • 举报
回复
上面只要把pwd改成数组就行了!给里面加!
jiangjinlinok 2011-07-07
  • 打赏
  • 举报
回复

/**
* 生成随即密码
* @param pwd_len 生成的密码的总长度
* @return 密码的字符串
*/
public String genRandomNum(int pwd_len)
{
//61是因为数组是从0开始的,52个字母+10个数字
final int maxNum = 62;
int i; //生成的随机数
int count = 0; //生成的密码的长度
char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' ,'L', 'M' ,'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while(count < pwd_len)
{
//生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(maxNum)); //生成的数最大为62-1

if (i >= 0 && i < str.length)
{
pwd.append(str[i]);
count ++;
}
}

return pwd.toString();
}

「已注销」 2011-07-07
  • 打赏
  • 举报
回复

public int[] getArrayInt()
{
int[] sort = new int[54];
int[] val = new int[54];
Random r = new Random();
for (int i = 0; i < 53; i++)
{
sort[i] = r.Next(1000);
val[i] = i+1;//这里刚才木有看清,是1~54
}

Array.Sort(sort, val);
return val;
}



「已注销」 2011-07-07
  • 打赏
  • 举报
回复


public int[] getArrayInt()
{
int[] sort = new int[54];
int[] val = new int[54];
Random r = new Random();
for (int i = 0; i < 53; i++)
{
sort[i] = r.Next(1000);
val[i] = i;
}

Array.Sort(sort, val);
return val;
}
Height86 2011-07-06
  • 打赏
  • 举报
回复
说真的 正是打算做一个扑克的小玩意

借楼主的帖子学习了

谢谢大家
happy20101027 2011-07-06
  • 打赏
  • 举报
回复
有解决方案就行了。
gallop1 2011-07-06
  • 打赏
  • 举报
回复
一楼的解法是有错误的,他的随机数只是最大是53,但是产生的随机数组是有可能重复的,所以,这样做是不对的,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
/*
* 定义了一个数组:int[] num=new int[54]
从1-54中随机排列到这个数组中,每一个数都不相同,也就是说让1-54这54个数随机分配到数组中应该怎样实现呢??
*/
class Program
{
static void Main(string[] args)
{
int[] num = new int[54];
List<int> list = new List<int>(54);
while(list.Count<54)
{
Random r = new Random();
int temp = r.Next(1,55);//随机数组的范围,界定他们的范围,大于等于minValue,小于maxValue,返回包含minValue的值
if(!list.Contains(temp))
{
list.Add(temp);
}
}

PrintValue(list);
Console.WriteLine();
}

/// <summary>
/// 打印相关的数据
/// </summary>
/// <param name="list"></param>
private static void PrintValue(List<int> list)
{
//打印相关的数据
Console.WriteLine("list的数组如下:");
for (int i = 0; i < list.Count; i++)
{
Console.Write(list[i] + "\t");
}
}
}
}
解法二:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class TestRandom2
{
/*
* 定义了一个数组:int[] num=new int[54]
从1-54中随机排列到这个数组中,每一个数都不相同,也就是说让1-54这54个数随机分配到数组中应该怎样实现呢??
*/
static void Main(string[] args)
{
//第二种解法
int[] num = new int[54];
SortedList sortList = new SortedList(54);
Random r = new Random();
while (sortList.Count < 54)
{
int temp = r.Next(1, 200);
if (!sortList.Contains(temp))
sortList.Add(sortList.Count+1, temp);
}
Console.WriteLine();
Console.WriteLine("数组如下:");
Print(sortList);
}

public static void Print(SortedList list)
{
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0},{1}",list.GetKey(i),list.GetByIndex(i));
}
}
}
}
kingdom_0 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 caozhy 的回复:]
给你一个简单而且易懂的代码,代码只要1行:

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Mai……
[/Quote]int类型的数组可以么?应该转换一下。
huangwenquan123 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gallop1 的回复:]
一楼的解法是有错误的,他的随机数只是最大是53,但是产生的随机数组是有可能重复的,所以,这样做是[/Quote]
谁说最大53的,随机最大53是没错,不能取到上限,可是在list中取也就是0-53
至于重复更不可能,每次取出一个值后list中就把那个值删掉了,何来重复之说?
先测试了在说别人正不正确!
kaiaiyaya 2011-06-29
  • 打赏
  • 举报
回复
哇 CSDN真给力 基本都能实现
SoaringSnake 2011-06-29
  • 打赏
  • 举报
回复
虽然办法笨了点,但是经过测试,是可以实现的,而且没涉及到任何泛型之类的
SoaringSnake 2011-06-29
  • 打赏
  • 举报
回复
public int[] GetRandArray(int min, int max)
{
int[] num = new int[max - min+1];
Random rand = new Random();
for (int i = min; i <= max; i++)
{
num[i - min] = RandNum(rand, min, max, num);
}
return num;
}

int RandNum(Random rand, int min, int max, int[] num)
{
int j = rand.Next(min, max+1);
bool success = true;
foreach (int item in num)
{
if (item == j)
{
success = false;
break;
}
}
if (!success)
{
j = RandNum(rand, min, max, num);
}
return j;
}
lihanbing 2011-06-29
  • 打赏
  • 举报
回复
        static int[] RandomArray(int count)
{
List<int> list = new List<int>();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 1; i <= count; i++)
{
int r = rnd.Next(0, list.Count + 1);
list.Insert(r, i);
}
return list.ToArray();
}
ycproc 2011-06-29
  • 打赏
  • 举报
回复
最简单的GUID.NewGuid();
老毕 2011-06-29
  • 打赏
  • 举报
回复
Random rand = new Random();
List<int> temp = new List<int>();

while (temp.Count < 54)
{
// Random()能取下界值,但不能取上界值,所以是1~55
int i = rand.Next(1, 55);

if (!temp.Contains(i))
temp.Add(i);
}

int[] result = temp.ToArray();

foreach (int i in result)
Console.WriteLine(i);

结果:

11
3
38
21
32
47
8
26
19
6
52
42
39
23
48
4
43
40
7
31
28
36
25
54
45
2
10
22
24
14
16
18
49
27
13
5
37
34
12
35
50
1
53
15
33
51
30
20
29
17
9
46
44
41
threenewbee 2011-06-29
  • 打赏
  • 举报
回复
给你一个简单而且易懂的代码,代码只要1行:

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] num = Enumerable.Range(1, 54).Select(x => new { Key = Guid.NewGuid().ToString(), Value = x }).ToList().OrderBy(x => x.Key).ToArray();
data.ToList().ForEach(x => Console.Write(x.Value + " "));
}
}
}
加载更多回复(3)

110,546

社区成员

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

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

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