Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Non-repeating random selection & arrays????

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi all!
I hope someone can help me.

I've got a script that returns a random recordset. I loop throught the database a specified number of times and need to have a unique return each time.

I just can't seem to get my head around the concept of arrays and I think that is what I need to do to get this done!

Here is some code:

' begin random function
randomize
'Set your Min and Max Values
CollisionRandom_Number_Min = 001001
CollisionRandom_Number_Max = 001025

' random numbers is the varible that will contain a numeric value
Random_Number = Int(((CollisionRandom_Number_Max-CollisionRandom_Number_Min+1) * Rnd) + CollisionRandom_Number_Min)

CollisionCommand = "SELECT * FROM question LEFT OUTER JOIN answer " & _
"ON question.index=answer.index WHERE CInt(Question.Index) = '"&random_number&"'"
' ORDER BY Question.Index,Response"

So....I guess my question is:

How to I specify in my CollisionCommand statement to get the random Question.Index, but only if it hasn't already been used??

Does this make sense??

Thanks to all who help!
 
It makes sense. You want to retrieve them in random order but without repetitions. Correct?
If so, you need to store each generated number in some structure, i.e. an array, and then check whether you already used the number.

Dim myNums(1000)
'then init it with zeros

for i=1 to 500 'hoewever many questions you'd like to ask
do
currNum = getRandom()
loop while wasUsed(currNum)
myNums(i)=currNum
'and go on doing what you were doing... ;)
next 'i

function wasUsed(n)
wasUsed=false;
for i=1 to 1000
if myNums(i)=n then
wasUsed=true
end if
next 'i
end function

function getRandom()
getRandom = Int(((CollisionRandom_Number_Max- CollisionRandom_Number_Min+1) * Rnd) + CollisionRandom_Number_Min)
end function

Was this helpful?

---
---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top