110,546
社区成员
发帖
与我相关
我的任务
分享
/**
* 生成随即密码
* @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();
}
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;
}
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;
}
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();
}
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
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 + " "));
}
}
}