asp生成不重複的隨機數,隨機取資料庫記錄

2021-04-13 09:09:10 字數 1129 閱讀 1693

<%

'生成 k 個不重複的 小於max 的隨機數

dim k

k=4max=10

dim b()    '生成的數儲存在陣列b()中

redim b(k)

b(0)=0

function check(t,n)

check=true

for d=1 to n-1

if t=b(d) then

check=false

end if

next

end function

for i= 1 to k

dorandomize  

b(i)= int(rnd*max)+1

loop until check(b(i),i)

next

%>

另一種方法:

由陣列a記錄某數是否已生成,避免重複

<%

dim a(),b()

redim a(40),b(40)

for i=1 to 40

a(i)="noselected"

next

k=20

for j=1 to k        do

randomize

b(j)= int(rnd*40) 1

if a(j)="noselected" then

exit do

end if

loop

next

for i=1 to k

response.write "

"&b(i)

next

%>

這個方法可用來從資料庫中取隨機記錄,具體如下:

先建立資料集記錄rs ,令 max=rs.recordcount 生成陣列 b 後

str=join(b,"|")

str="|"&str&"|"

n=1while not rs.eof

if instr(str,"|"&n&"|")  then

'進行操作

不重複隨機數生成

直接上 生成 0,total 的隨機數 最大隨機數 ilist private static ilistgetrandomsequence int total random random new random for var i listbase.count 1 i 0 i return listo...

生成不重複的隨機數

思想是用乙個陣列來儲存索引號,先隨機生成乙個陣列位置,然後把這個位置的索引號取出來,並把最後乙個索引號複製到當前的陣列位置,然後使隨機 數的上限減一,具體如 先把這100個數放在乙個陣列內,每次隨機取乙個位置 第一次是1 100,第二次是1 99,將該位置的數用最後的數 代替。int index n...

隨機數生成(永不重複)

public class randomtreeutils char chars new char char chars new char int treedeeps chars.length int key 7 最多生成7位數 if treedeeps key 返回樹形 arraylist tree...