vb 中怎麼用隨機函式產生10 100之間的互不相同的

2021-12-19 02:38:00 字數 6641 閱讀 1860

1樓:秋色烽火

思路是先設定一個定額陣列 要取的值一一對應位置,每次抽取隨機量就對比對應位置的值是否為0 如果不為0 就取 並置為0

直到取完為止

注意的是 待抽陣列的長度一定要大於或等於要抽的數量 否則有可能進入死迴圈

private sub form_click()'定義一個陣列,兩個變數

dim a%(90), rnum%, rid%'迴圈90次

for i = 0 to 90

'為陣列賦值,每個值為當前i+10 即對應10-100a(i) = i + 10

next

'do while迴圈 條件為小於50

do while rid < 51

randomize '初始化隨機種子

rnum = int(rnd * 91) '生成隨機量 0-90if a(rnum) <> 0 then '判斷對應陣列位置值是否為0

print rnum '不為0 輸出

a(rnum) = 0 '並置對應位為0

rid = rid + 1 '計數器加1

end if

doevents '返還系統控制

loop

end sub

2樓:匿名使用者

最簡單的方法就是利用陣列,產生一個數以後就判斷前面是否已經有這個數,如果有,那麼重新生成這個數。

private sub form_click()dim a(1 to 50) as integer, i as integer, j as integer

randomize

for i = 1 to 50

a(i) = int(rnd * 91 + 10)for j = 1 to i - 1

if a(i) = a(j) then i = i - 1next j

next i

for i = 1 to 50

print a(i);

if i mod 5 = 0 then printnext i

end sub

3樓:匿名使用者

給你寫個函式吧

'生成從istar開始(包括)到iend結束(包括)的icount個整數的序列,儲存到n() as integer陣列裡

private sub numrnd(istar as integer, iend as integer, icount as integer, n() as integer)

dim i, j

redim n(icount - 1) as integer

randomize

n(0) = int(rnd * (iend - istar + 1)) + istar

for i = 0 to icount - 1

n(i) = int(rnd * (iend - istar + 1)) + istar

for j = 0 to i - 1

doevents

if n(j) = n(i) then i = i - 1: exit for

next j

next i

end sub

使用方法

dim k() as integer

numrnd 50, 100, 10, k '生成10個50到100的整數

4樓:匿名使用者

private sub command1_click()dim a(1 to 50) as integer, i as integer, j as integer

for i = 1 to 50

randomize

a(i) = int(rnd * 90 + 10)for j = 1 to i - 1

if a(i) = a(j) then i = i - 1next j

next i

for i = 1 to 50

print a(i);

n=n+1

if n mod 5 = 0 then 『每行輸出5個數

print

end if

next i

end sub

5樓:匿名使用者

private sub form_click()randomize

dim a(50) as integer

for i = 1 to 50

a(i) = int(rnd * 90) + 10next i

for i = 1 to 50

if i mod 10 = 0 then

print a(i);

print

else

print a(i); " ";

end if

next i

end sub

6樓:勇哥和嫻妹

randomize()

for i=10 to 100

form1.print int(rnd*(100-10+1)+10)next

7樓:匿名使用者

private sub form_click()dim a(49) as integer

dim b(10 to 100) as boolean'取數標誌

randomize

for i = 0 to 49

don = int(rnd * 91) + 10loop while b(n)

b(n) = true

a(i) = n

'找到未取的數,並放入陣列,設定標誌位

print a(i);

if i mod 10 = 9 then printnext i

end sub

vb程式設計:使用隨機函式產生50個10~100之間的互不相同的整數,存於一陣列中,並以升序每行10個數在**框上輸

8樓:聽不清啊

private sub command1_click()dim a(10 to 100) as booleanrandomize

n = 0

while n < 50

x = int(91 * rnd) + 10if not a(x) then

a(x) = true

n = n + 1

end if

wend

n = 0

for i = 10 to 100

if a(i) then

picture1.print i;

n = n + 1

if n mod 10 = 0 then picture1.print

end if

next i

picture1.print

end sub

vb程式設計,用隨機函式產生50個10~100之間的互不相同的整數存於一陣列中,並以升序每行10個數顯示在窗體上

9樓:

private sub command1_click()dim x(90) as integer

'高效**,產生無重複隨機數

for i = 10 to 100

x(i - 10) = i

next

randomize

for i = 1 to 50

j = int(rnd * (91 - i)) + it = x(i)

x(i) = x(j)

x(j) = t

next

'排序for i = 1 to 50

for j = 1 to i

if x(i) < x(j) then t = x(i): x(i) = x(j): x(j) = t

next

next

'顯示for i = 1 to 50

print x(i);

if i mod 10 = 0 then printnext

end sub

10樓:

private sub command1_click()dim numa(50) as integer, tmp1 as long, tmp2 as long

for tmp1 = 1 to 50

numa(tmp1) = rnd() * 90 + 10next

numa(0) = numa(1)

for tmp1 = 1 to 49

for tmp2 = tmp1 to 50if numa(tmp2) < numa(tmp1) thennuma(0) = numa(tmp2)

numa(tmp2) = numa(tmp1)numa(tmp1) = numa(0)

end if

next

if tmp1 mod 10 = 0 thenprint numa(tmp1)

else

print numa(tmp1);

end if

next

print numa(50)

end sub

vb用隨機函式產生50個【10-100】的隨機整數,並按照從小到大的順序列印出來

11樓:匿名使用者

dim a(50) as integer

private sub command1_click()randomize

for i = 1 to 50

a(i) = int(rnd * 90 + 10)next i

picture1.cls

picture1.print "排序前資料:"

for i = 1 to 50

picture1.print a(i);

if i mod 10 = 0 then picture1.print

next

end sub

private sub command2_click()for i = 1 to 49

max = a(i)

for j = i + 1 to 50

if a(j) < max then

max = a(j)

temp = a(i): a(i) = a(j): a(j) = temp

end if

next

next

picture1.print "排序後資料:"

for i = 1 to 50

picture1.print a(i);

if i mod 10 = 0 then picture1.print

next

end sub

在 vb中,利用隨機函式產生 10個 1---100 之間的隨機整數,找出其中能被 5 整除的數並求其和

12樓:匿名使用者

private sub command1_click()

text1.text = ""

dim aa(1 to 20) as integer, ss as integer, sda as integer

for i = 1 to 20

randomize

aa(i) = int(rnd * 99 + 1)

next i

for i = 1 to 19

for j = 1 to 20 - i

if aa(j) > aa(j + 1) then

sda = aa(j)

aa(j) = aa(j + 1)

aa(j + 1) = sda

end if

next j

next i

text1.text = text1.text & "生成的20個隨機數:" & vbcrlf

for i = 1 to 20

if aa(i) < 10 then

text1.text = text1.text & "0" & aa(i) & space(3)

else

text1.text = text1.text & aa(i) & space(3)

end if

if i mod 10 = 0 then

text1.text = text1.text & vbcrlf

end if

next i

text1.text = text1.text & vbcrlf

text1.text = text1.text & vbcrlf

text1.text = text1.text & "能夠被5整除的數:" & vbcrlf

for i = 1 to 20

if aa(i) mod 5 = 0 then

text1.text = text1.text & aa(i) & space(3)

ss = ss + aa(i)

end if

next i

text1.text = text1.text & vbcrlf

text1.text = text1.text & vbcrlf

text1.text = text1.text & "能夠被5整除的數的總和是:" & vbcrlf

text1.text = text1.text & ss

end sub

VB Randomize函式怎麼產生隨機的數

1全部randomize 用當前時間初始化隨機種子x rnd x裡就是隨機數一個 randomize 預設的seed就是當前時間,一定要加seed用randomize timer 和上句作用相同。例如在100裡隨機生成一個整數 randomize a rnd 101 b fix a msgbox b...

matlab隨機函式怎麼取,matlab中怎樣隨機從一組資料中取一個數

如果是要要均勻分佈,可以使用語句rand 1 0.8 0.1 matlab中的rand函式 用於產生隨機數 均勻分佈的隨機數或矩陣 語法y rand n y rand m,n y rand m n y rand m,n,p,y rand m n p.y rand size a rand s rand...

VB呼叫函式過程,VB中的函式過程怎麼呼叫??

function gys byval x as integer,byval y as integer as integer do while y 0 preminder x y x yy preminder debug.print x,y 這樣你就能看清楚計算過程loop gys x end fun...