随机数生成?

xwyxwyujj 2011-01-10 02:53:25
随机生成100个6位到10位的随机数
有重复的去掉 这个方法怎么写啊?
...全文
172 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hertz_liu 2011-01-10
  • 打赏
  • 举报
回复

void Main()
{
for(int i=0;i<100;i++)
{
Console.WriteLine(Guid.NewGuid().ToString().Replace("-","").Substring(0,6));
}
}

以上是一楼的代码,我觉得再1楼的改进一下,就能100%满足楼主的要求了。

void Main()
{
for(int i=0;i<100;i++)
{
//加一个随机6-10的数字
Random ran=new Random();
int len=ran.Next(6,10);

Console.WriteLine(Guid.NewGuid().ToString().Replace("-","").Substring(0,len));
}
}
rczjp 2011-01-10
  • 打赏
  • 举报
回复
int[] iarr = new int[100];
ArrayList arrlist = new ArrayList();
Random rnd = new Random();
while (arrlist.Count <= 100)
{
int num = rnd.Next(100000, int.MaxValue);
if (!arrlist.Contains(num))
{
arrlist.Add(num + " ");
}
}
for (int i = 0; i < 100; i++)
{
Console.Write(arrlist[i]);
}
EvenWen 2011-01-10
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
Random dr = new Random();
List<int> list = new List<int>(101);
for (int k = 0; k <=list.Count; k++)
{
for (int i = 0; i <= list.Count; i++)
{
int resut = dr.Next(100000, 99999999);
for (int j = 0; j <= list.Count; j++)
{
if (!list.Contains(resut))
{
list.Add(resut);

}


}

}

Console.WriteLine(list[k]+"\n");
}

}
EvenWen 2011-01-10
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
Random dr = new Random();
List<int> list = new List<int>(101);
for (int k = 0; k <=list.Count; k++)
{
for (int i = 0; i <= list.Count; i++)
{
int resut = dr.Next(100000, 99999999);
for (int j = 0; j <= list.Count; j++)
{
if (!list.Contains(resut))
{
list.Add(resut);

}


}

}

Console.WriteLine(list[k]+"\n");
}

}
callmesai 2011-01-10
  • 打赏
  • 举报
回复

private static char[] constant_justNumber ={ '0','1','2','3','4','5','6','7','8','9'};


/// <summary>
/// 生成随机数
/// </summary>
/// <param name="Length"></param>
/// <param name="Count"></param>
/// <param name="hasBig"></param>
/// <returns></returns>
public static string[] GenerateRandom(int Length, int Count)
{
int myLength = constant_justNumber.Length;
char[] constant = new char[myLength];
string[] myOutArr = new string[Count];


System.Text.StringBuilder newRandom = new System.Text.StringBuilder(constant.Length);
Random rd = new Random();
for (int j = 0; j < Count; j++)
{
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(constant.Length)]);
}
myOutArr[j] = newRandom.ToString();
newRandom.Remove(0, Length);
}


return myOutArr;
}

protected void Button1_Click(object sender, EventArgs e)
string[] myArr = GenerateRandom(10, 100);
int h = 0;
foreach (string w in myArr)
{
h++;
Response.Write(w + "<br/>");

}
Response.End();
}
逆向 2011-01-10
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
test();
}
}
private void test()
{
Random rdm = new Random();
List<string> list = new List<string>();
for (int i = 1; i > 0; i++) //死循環 在for裏面用if(list.Count < 100)來控制跳出循環
{
string temp = "";
if (list.Count < 100)
{
int a = rdm.Next(6, 11);//随机来控制生产几位数的随机数
#region 根据a(位数)来(生成0到9的数字)组合随机数
for (int b = 0; b < a; b++)
{
temp += rdm.Next(0, 9).ToString();
}
#endregion

#region 判斷當前生成的隨機數和之前的是否重複
for (int j = 0; j < list.Count; j++)
{
if (list[j].Equals(temp))
{
list.Remove(temp); //當重複的時候remove存在的
}
}
list.Add(temp);
#endregion
}
else
{
break; //生成完100個隨機數 跳出for
}
}
foreach (string m in list)
{
Response.Write(m.ToString()+"<BR>");
}
}
}

//看我如此细心 分也是我的了
laowang134 2011-01-10
  • 打赏
  • 举报
回复
额 超出最大int值了。。
int result = rd.Next(100000, 2147483647);
楼主可以根据此方法组合成10位数
逆向 2011-01-10
  • 打赏
  • 举报
回复
回复3楼
回复9楼

你们的好像不可能产生10位的随机数
laowang134 2011-01-10
  • 打赏
  • 举报
回复
if (list.Count < 101)
应该是小于100
laowang134 2011-01-10
  • 打赏
  • 举报
回复
去重复的

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

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Random rd = new Random();
List<int> list = new List<int>();
for (int i = 0; i < 1000; i++)
{
if (list.Count < 101)
{
int result = rd.Next(100000, 999999999);
for (int j = 0; j < list.Count; j++)
{
if (list[j] == result)
{
list.Remove(result);
}
}
list.Add(result);
}
else
{
break;
}
}
foreach (int i in list)
{
Console.WriteLine(i);
}
}
}
}

laowang134 2011-01-10
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
Random rd=new Random();
List<int> list=new List<int>();
for(int i=0;i<1000;i++)
{
if(list.Count<101)
{
int result=rd.Next(100000,999999999);
for(int j=0;j<list.Count;j++)
{
if(list[j]==result)
{
list.Remove(result);
}
}
list.Add(result);
}
else
{
break;
}
}
foreach (int i in list)
{ Console.WriteLine(i); }
}
wuyq11 2011-01-10
  • 打赏
  • 举报
回复
Random rand = new Random(Guid.NewGuid().GetHashCode());
rand .Next(最小值,最大值)
Random rnd = new Random(DateTime.Now.Millisecond);

Enumerable.Range(1, 100).OrderBy(Guid.NewGuid());
for (int i = 0; i <= 9; i++)
{
Console.WriteLine(rand.Next(1,100));
}

Enumerable.Range(1, 10).OrderBy(Guid.NewGuid());
守护欢笑 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 q107770540 的回复:]
C# code

void Main()
{
Random rd=new Random();
for(int i=0;i<100;i++)
{
Console.WriteLine(rd.Next(100000,999999999).ToString());
}
}

/*
612809168
307026325
238081079
835900550
……
[/Quote] +1
xwyxwyujj 2011-01-10
  • 打赏
  • 举报
回复
重复的怎么去掉呢
q107770540 2011-01-10
  • 打赏
  • 举报
回复

void Main()
{
Random rd=new Random();
for(int i=0;i<100;i++)
{
Console.WriteLine(rd.Next(100000,999999999).ToString());
}
}

/*
612809168
307026325
238081079
835900550
58182993
968161134
865417802
91417747
359756267
669914075
476542297
140429046
73965256
335856911
150959830
489977153
707831345
857504989
418352885
46806794
883680
924778950
936652240
67844623
797614593
34880721
959088475
281230786
253769050
557078969
952499867
127289537
98133238
242658759
215979328
259152563
889214270
285335260
383666935
230718403
416753301
51368580
360718244
168791762
864311800
858096752
205983516
155920300
543153219
841802429
345607692
708887622
967286605
461155477
31978985
688030216
370374084
170336455
38385956
23402272
9172659
584287016
837648696
802677297
717414207
349352759
42395808
831306495
119977583
891807266
600762881
422596084
473938053
187734481
630053492
949515098
564160706
767960478
203532822
939517840
828897204
803268175
738077565
411966620
211571277
243712244
160002931
636977760
210779774
527949110
888778477
718977814
247049304
360364663
221645743
832466283
213719883
558040946
451377553
515059040

*/

xwyxwyujj 2011-01-10
  • 打赏
  • 举报
回复
随机数只有数字
q107770540 2011-01-10
  • 打赏
  • 举报
回复

void Main()
{
for(int i=0;i<100;i++)
{
Console.WriteLine(Guid.NewGuid().ToString().Replace("-","").Substring(0,6));
}
}

/*
108efe
d7df9b
a1ce80
6edaaf
68bc2c
e3e5cb
f9754c
525dbd
ef71dd
fe4d99
fc493b
967fed
ee3dca
29a83f
f954ed
3213bd
08967a
61bce0
220ff0
2adda3
a28526
3c9005
1d31d9
17a6c7
a9c88e
fc5f50
7460a6
499919
4b7cfe
59e4fd
4ded30
afbfbb
79be83
af3b7e
b3ed20
0f5696
82e573
3d9911
2e489a
b708b2
4c6b62
5c7a03
6082b5
ccd0dd
f40410
aff068
946d07
2bb6e0
dfc234
35bc51
ee96f7
1b5448
1c0f8e
71eeef
b3184c
613b8e
2ed09c
7f2e82
77459d
1445b9
713440
eef8a1
2b5047
0de640
5f7700
ebd258
aedfbc
ecfaca
08fe4d
87efeb
e8e800
55f2e7
2d8730
c9eb03
66b69d
85126a
c43162
bbfaea
12dbe0
ee1c17
b51d42
0bfd0c
d57fb6
439f80
096b98
3e6130
bd64f7
f60560
43a601
b2f192
551229
f395e5
a94ee8
6ff113
464696
cd4c08
5a4a1f
d273d0
e46a69
d4bfea

*/

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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