我想取亂數,一開始只用rand,取數字的個數沒問題,但是每次序列相同。
經網友指點,在前面加了randomize(0),數字變隨機了,但是取數字的個數也變得隨機。大家遇到過這種問題嗎?
我這是個組卷系統,結果每一型別題的數量不確定了。
wf_catch(integer fi_amount,string fc_difficulty,string fs_type)
string fs_index[]
string fs_pointer
integer i,k,j,r,m,n
integer a_temp[]
integer flag
string ss
if fi_amount = 0 then
return
end if
i = 0
choose case fs_type
case "單項選擇題","多項選擇題","填空題"
declare my_cursor cursor for
select m_id
from t_objective
where m_type = :fs_type and m_difficulty = :fc_difficulty and m_exdate < :gd_allow and m_chapter not in
(select c_id from t_chapter where c_flag = 0);
open my_cursor;
fetch my_cursor into :fs_pointer;
if sqlca.sqlcode <> 0 then
messagebox("","不能正常使用游標")
return
else
do
i ++
fs_index[i] = fs_pointer
fetch my_cursor into :fs_pointer;
loop until sqlca.sqlcode = 100
end if
close my_cursor;
if i < fi_amount then
messagebox("","試題不夠組卷。")
return
end if
Randomize(0)
a_temp[1] = rand(i)
m = a_temp[1]
update t_objective
set m_flag = 1
where m_id = :fs_index[m];
if SQLCA.SQLCode <>0 THEN
MessageBox("SQL error", SQLCA.SQLErrText)
rollback using sqlca;
return
else
commit using sqlca;
end if
if fi_amount >1 then
k = 1
do while k < fi_amount
Randomize(0)
r= rand(i)
flag = 0
for j = 1 to k step 1
if a_temp[j] = r then
flag = 1
exit
end if
next
if flag = 0 then
k ++
a_temp[k] = r
m = a_temp[k]
update t_objective
set m_flag = 1
where m_id = :fs_index[k];
if SQLCA.SQLCode <>0 THEN
MessageBox("SQL error", SQLCA.SQLErrText)
rollback using sqlca;
return
else
commit using sqlca;
end if
end if
loop
end if
case "名詞解釋","簡答題","論述題"
declare new_cursor cursor for
select m_id
from t_subjective
where m_type = :fs_type and m_difficulty = :fc_difficulty and m_exdate < :gd_allow and m_chapter not in
(select c_id from t_chapter where c_flag = 0);
open new_cursor;
fetch new_cursor into :fs_pointer;
if sqlca.sqlcode <> 0 then
messagebox("",SQLCA.SQLErrText)
return
else
do
i ++
fs_index[i] = fs_pointer
fetch new_cursor into :fs_pointer;
loop until sqlca.sqlcode = 100
end if
close new_cursor;
if i < fi_amount then
messagebox("","試題不夠組卷。")
return
end if
Randomize(0)
a_temp[1] = rand(i)
m = a_temp[1]
update t_subjective
set m_flag = 1
where m_id = :fs_index[m];
if SQLCA.SQLCode <>0 THEN
MessageBox("SQL error", SQLCA.SQLErrText)
rollback using sqlca;
return
else
commit using sqlca;
end if
if fi_amount >1 then
k = 1
do while k < fi_amount
Randomize(0)
r= rand(i)
flag = 0
for j = 1 to k step 1
if a_temp[j] = r then
flag = 1
exit
end if
next
if flag = 0 then
k ++
a_temp[k] = r
m = a_temp[k]
update t_subjective
set m_flag = 1
where m_id = :fs_index[k];
if SQLCA.SQLCode <>0 THEN
MessageBox("SQL error", SQLCA.SQLErrText)
rollback using sqlca;
return
else
commit using sqlca;
end if
end if
loop
end if
case else
messagebox("","傳參錯誤。")
end choose
uj5u.com熱心網友回復:
randomize(0)中的0換成你要的個數試試看。uj5u.com熱心網友回復:
先確定題數,再在指定的題組中去隨機選取不重復的題號,裝入一陣列,再取題組卷.uj5u.com熱心網友回復:
我覺得你可以試試Randomize(數值)This statement sets the seed for the random number generator to 0 so that calls to Rand generate a new sequence each time the script is run:
Randomize(0)
This statement sets the seed for the random number generator to 4 so that calls to Rand repeat a specific sequence each time the random number generator is initialized:
Randomize(4)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/96419.html
標籤:腳本語言
上一篇:是行家就來看看。。。幫幫忙!
