像QQ那种申请号码随机,不重复的方法是怎么样的?
QQ号好像是前几位固定增长,后几位是随机的,这样有啥方法可以实现?必需随机入座100个,入库总数也是100个,不能少了。
我想的一种土办法:
在数据库里先生成临时的座位表,如100~200,然后随机入坐,入坐的数据再插入正式表里。
然后再随机入座。直到总数为100条后,清空临时座位表,生成200~300的,再次循环入座。。。
请问达人们,有啥更好的办法? --------------------编程问答-------------------- 楼主方法挺好
接分 --------------------编程问答-------------------- 不用等100个全用了再生成新的
50的时候就生成到100~150好了
保持可用座位的在50~100之间 --------------------编程问答--------------------
--------------------编程问答-------------------- 随机数啊。。
List<int> nums = new List<int>();
Random r=new Random();
for (int i = 0; i < 100;i++ )
{
int step = r.Next(25292201, 26000000);
if (!nums.Contains(step)) { nums.Add(step); }
}
//nums即得25292201至26000000(不含)的qq号
System.Random rnd = new System.Random();
IEnumerable<int> numbers = Enumerable.Range(0, 10).OrderBy(r => rnd.Next()).Take(3);
List<int> nums = numbers.ToList();
nums.ForEach(I => Response.Write(I.ToString() + "<br/>"));
JS随机数
方法1:
Js代码
// 要创建一个随机浮点数时,使用rnd();要创建一个随机整数时,例如,1到10时,使用rand(10)。
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};
function rand(number) {
return Math.ceil(rnd()*number);
};
// 要创建一个随机浮点数时,使用rnd();要创建一个随机整数时,例如,1到10时,使用rand(10)。
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};
function rand(number) {
return Math.ceil(rnd()*number);
};
方法2:
Js代码
function selectFrom(iFirstValue,iLastValue)
{
var iChoces = Math.abs(iLastValue - iFirstValue)+1;
return Math.floor(Math.random() * iChoces + iFirstValue);
}
for(var i=0;i<50;i++){
alert(selectFrom(1,6));
}
function selectFrom(iFirstValue,iLastValue)
{
var iChoces = Math.abs(iLastValue - iFirstValue)+1;
return Math.floor(Math.random() * iChoces + iFirstValue);
}
for(var i=0;i<50;i++){
alert(selectFrom(1,6));
} 方法3
Js代码
//声明一个随机数变量,默认为1
var GetRandomn = 1;
//获取随机范围内数值的函数
function GetRandom(n){GetRandomn=Math.floor(Math.random()*n+1)}
//开始调用,获得一个1-100的随机数
GetRandom("100");
//输出查看
document.write(GetRandomn)
</script>
是不是相当简洁的代码呢?GetRandomn就是随机数的变量,可以任意调用了。
举个随机显示的特效代码吧:
<script>
//现在开始,首先获得一个1到3的随机数
GetRandom("3");
//声明一个随机数变量,默认为1
var GetRandomn = 1;
//获取随机范围内数值的函数
function GetRandom(n){GetRandomn=Math.floor(Math.random()*n+1)}
//开始调用,获得一个1-100的随机数
GetRandom("100");
//输出查看
document.write(GetRandomn)
</script>
是不是相当简洁的代码呢?GetRandomn就是随机数的变量,可以任意调用了。
举个随机显示的特效代码吧:
<script>
//现在开始,首先获得一个1到3的随机数
GetRandom("3");
来个范围的
例如 1-6
Js代码
alert(parseInt(Math.random()*(6)+1));} --------------------编程问答-------------------- 再顶下,没好办法,就结了。。 --------------------编程问答-------------------- 楼主方法很好 --------------------编程问答--------------------
这个没理解LZ想要什么样的。。
笨方法。。
就是在数字A和数字B中间随机。。
再把随机出来的数在数据库表中查询一下。。。看返回的count是不是大于1.。
大于1就有了。。。再随 --------------------编程问答-------------------- list<int> num = new list<int>();
for(int i=0;i++;i<100)
{
num.add(i);
}
取号码
int getnum()
{
int time = 当前毫秒数;
int theindex = time /num.count;\\随机当前list中的一个值
return num[theindex];
} --------------------编程问答-------------------- 改一下错了
取号码
int getnum()
{
int time = 当前毫秒数;
int theindex = time /num.count;\\随机当前list中的一个值
int thenum = num[theindex];
num.remove(theindex);\\删除已用的
return thenum;
} --------------------编程问答--------------------
Random r = new Random();
List<int> lists = new List<int>();
for (int i = 0; i < 100; i++)
{
int k=r.Next(1,100);
if (lists.IndexOf(k) > 0)
{
i--;
}
else
{
lists.Add(k);
}
}
楼主能明白 吗?不明白在问我。QQ群 1064 97038 --------------------编程问答-------------------- 腾讯用的就是lz的方法 --------------------编程问答--------------------
内部人员啊 --------------------编程问答--------------------
真的哦!
补充:.NET技术 , ASP.NET