我知道你什么意思了

来源: 2006-12-01 21:22:36 [旧帖] [给我悄悄话] 本文已被阅读:

你是想产生0-5000的最大随机序列,要求不重复。算法的优劣是看你出现重复的次数和序列的长度。序列越长、时间越短成绩越好。

最快的方法应该是Hashing。先产生一个顺序数组0-5000 A(5001个),然后建一个5001元素的空数组B,自己去找一个较好的Hashing 函数一般是(x*m+y*n)mod p,其中p是relative prime to x,y,m,n。这些都是教科书式的公式,一般不难找。

Hashing function 选好以后从A中顺序取数送入Hash(A[i]),产生结果用LinkList方式添入B。如果有重复则push in link list header。真实序列长度=5001-# collision。In this, hashing is really acting like random reordering. Question is: How to make it truly random every run-time.

While you can use rand(current time) to get first number, in our case x, then y = rand(x), m = rand(time), n =rand(n). The find relative prime P. Then go back to use Hash(A[i]) again.