|
-
Jul 9th, 2001, 02:38 AM
#1
Create Vars on run time?
Hey,
I'm using the random function to randomize files from a listbox.
There r x files in the listbox, and i want that if the random number is 13, file 13 will open, and then the random function WONT open that 13 file ne more.
In This way when the random function randomize x files (as the max of the listbox), it will quit. (It turns out that every file got opened only 1 time)
ne way how to save the randomized numbers?
I thought about creating as much vars as the listbox.count and then save every random number to a var...
is this possible, or is there an easier way to do this?
tnx
-
Jul 9th, 2001, 03:03 AM
#2
You could save them in a dynamic array - an array that you can reset the boundaries / size of.
Code:
Private arrNumbers() as integer
Private intCounter as integer
'Set up the array - not specifying a limit / Ubound entry
'Set up a counter for the Ubound value of the array
Private Sub Form Load()
intCounter = 1
End Sub
Private Function RandNum(byval ListBox_Num as integer) as integer
RandNum = ((int(listBox_Num) / 5) * Rnd) * 6
'Generate a random number from the listbox
Redim arrNumbers(intCounter)
'Re-size the array - set the max number of cells as the
'number of the counter variable
arrNumbers(intCounter - 1) = RandNum
'add the random number to the array
intCounter = intCounter + 1
'update the counter for next time the function's called
End Function
Once that's done, you can save these to a database, or file. I'll do a quicky example of saving these to a file :
Code:
Dim intCounterLoop as integer
If len(dir$("C:\Path\File.txt")) <> 0 then
'File doesn't already exist, continue ...
Open "C:\Path\File.txt" for output as #1
'Create and open the text file for writing to
For intCounterLoop = 0 to (intCounter -1)
'set up a loop to go through each array entry
Print #1, arrNumbers(intCounterLoop) & VBCrlf
'write the current random number to the text file,
'then drop down to a new line in the text file
Next intCounterLoop
Close #1
Msgbox "File has been completed"
Finally, you'll want to put this in :
Code:
Private Function RandNum(byval ListBox_Num as integer) as integer
If (intCounter) = listbox.items.count then
'This has gone past the last listbox entry. The function will
'produce an error & you need this app to close anyway, so
'you can do that part here ...
Msgbox "The last file has been reached !"
End If
'other code here ...
End Function
Last edited by alex_read; Jul 9th, 2001 at 03:13 AM.
-
Jul 9th, 2001, 03:04 AM
#3
PowerPoster
You can add the number to an array. Then when u get the next random number, search through the array, and if it is in there, then you have to pick a different number. If not, you add that number to the array. That's the way I would do it.
-
Jul 9th, 2001, 03:23 AM
#4
This one'll answer the bit about not going through the same number twice as well.
http://161.58.186.97/showthread.php?...ghlight=random
My job here is done, astalavista !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|