Results 1 to 4 of 4

Thread: Create Vars on run time?

  1. #1
    Stiletto
    Guest

    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

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    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.
    <removed by admin>

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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 !

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width