Results 1 to 9 of 9

Thread: a small problem

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32

    Unhappy

    My main program form is always running, among other buttons it has a button which you can click which brings up another form containing a listbox, and calls a sub on the new form which generates a random amount of random strings, but when i close the listbox form and load it again from the main program the strings are randomly generated again(obviously) can anyone tell me how to stop this?
    CGB

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    why don't you just hide the form, instead of unloading it, then the values in the listbox will be the same?

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    i do have reasons but i wont go into them because there are loads and i'd have to start combing the programs code, writing them down
    CGB

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    You have to save them yourself

    Obviously if you do not want the same sequence every time you run your program, and if you do not want to get a new sequence every time you open your form2, you will have to save the random sequence of strings the first time you enter form2.

    The only alternative is to seed VB's Randomiser yourself and reuse the same value each subsequent time.

    This will allow you to keep the same sequence of numbers without storing them in memory (only storing the seed number). This will keep the same sequence until the next time you restart the application. If this is what you want, then use something like the following code.

    Code:
    ' form1 has a single command button
    Option Explicit
    
    Private Sub Command1_Click()
      Form2.Show
    End Sub
    Code:
    ' form2 has a single command button and a list box
    Option Explicit
    
    Private Sub Command1_Click()
      Unload Me
    End Sub
    
    Private Sub Form_Load()
      Static seed As Double
      ' note that we use the timer for a seed value which is 
      ' the same as VB does if Randomize is called with 
      ' no parameter. 
      If seed = 0 Then seed = Timer
    
      Dim c As Integer
      Dim d As Integer
      Dim lenString  As Integer
      Dim tmp As String
    
      ' little known 'trick' with rnd and randomize keywords.
      ' call rnd with any negative parameter
      Rnd (-1)
      ' then call randomize with any positive seed
      Randomize seed
      ' and the sequence of random numbers will always be the
      ' same.  We ensure the sequence is different each time
      ' the app is run by using the timer as out seed.  If we
      ' use a constant value instead, then the "random" sequence
      ' will forever be the same
    
      ' generate 10 to 30 strings
      For c = 1 To Rnd() * 20 + 10
        tmp = ""
        ' get a random length between 5 and 15 chars
        lenString = Rnd() * 10 + 5
        For d = 1 To lenString
          ' get a random char form A to Z
          tmp = tmp & Chr(Rnd() * 25 + Asc("A"))
        Next
        ' add string to listbox
        List1.AddItem tmp
        ' print string also
        Debug.Print c, tmp
      Next
    End Sub
    Hope it helps
    Paul Lewis

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    this is basically what i am doing but it doesn't work (it wont add the strings when the form is loaded again)

    Sub GENITEMZ()

    If udtA.DONEGENERATE = 0 Then
    Employeez.Clear
    Empler.Clear
    HMNY = Int((8 * Rnd) + 1)
    Dim Counter As Integer
    Do Until Counter = HMNY
    Randomize
    RAN = Int((8) * Rnd)
    If RAN = 0 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 1 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 2 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 3 Then Employeez.AddItem ("Manual Labourer")
    If RAN = 4 Then Employeez.AddItem ("Bodyguard")
    If RAN = 5 Then Employeez.AddItem ("Bodyguard")
    If RAN = 6 Then Employeez.AddItem ("Scientist")
    If RAN = 7 Then Employeez.AddItem ("Navigator")
    Counter = Counter + 1


    Loop
    udtA.DONEGENERATE = 1
    End If

    Employeez.ListIndex = 0
    udtEmployList.Emp1 = Employeez.Text
    If Employeez.ListIndex > 0 Then
    Employeez.ListIndex = 1
    udtEmployList.Emp2 = Employeez.Text
    End If
    If Employeez.ListIndex > 1 Then
    Employeez.ListIndex = 2
    udtEmployList.Emp3 = Employeez.Text
    End If
    If Employeez.ListIndex > 3 Then
    Employeez.ListIndex = 3
    udtEmployList.Emp4 = Employeez.Text
    If Employeez.ListIndex > 4 Then
    Employeez.ListIndex = 4
    udtEmployList.Emp5 = Employeez.Text
    End If
    If Employeez.ListIndex > 5 Then
    Employeez.ListIndex = 5
    udtEmployList.Emp6 = Employeez.Text
    End If
    If Employeez.ListIndex > 0 Then
    Employeez.ListIndex = 6
    udtEmployList.Emp7 = Employeez.Text
    End If
    End If

    If udtA.DONEGENERATE = 1 Then
    Employeez.AddItem (udtEmployList.Emp1)
    Employeez.AddItem (udtEmployList.Emp2)
    Employeez.AddItem (udtEmployList.Emp3)
    Employeez.AddItem (udtEmployList.Emp4)
    Employeez.AddItem (udtEmployList.Emp5)
    Employeez.AddItem (udtEmployList.Emp6)
    Employeez.AddItem (udtEmployList.Emp7)
    End If



    End Sub
    CGB

  6. #6
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Reset udtA.DONEGENERATE to 0 again

    Based on what I see in your sample, I suspect you are not resetting your udtA.DONEGENERATE value to 0.

    This would prevent your code from generating the strings. Don't be fooled by the fact that your combo boxes are empty. This is due to the form being reloaded so of course they will be empty.

    You will also improve your code a whole bunch by implementing Select Case instead of your if statement block.

    Think about the value of your RAN variable. Can it ever equal 0 and 1 at the same time? If not (and I assure you it cannot) then you are wasting time in your code by asking whether RAN = 0 to 7 when we know that only one of these will be true.

    change the if statements to:
    Code:
    Dim RAN As Integer
    RAN = Int((8) * Rnd)
    Select Case RAN
    Case 0 To 3
      Employeez.AddItem ("Manual Labourer")
    Case 4, 5
      Employeez.AddItem ("Bodyguard")
    Case 6
      Employeez.AddItem ("Scientist")
    Case 7
      Employeez.AddItem ("Navigator")
    End Select
    Counter = Counter + 1
    Finally, before my advice is over, you only need to call Randomize once in your sub. Put it outside the loop (above the do statement is fine).

    Get into the habit of using Dim to decalre all variables. To get you into the habit, click "Require Variable Declaration" in the Tools | Option dialog in VB.

    Sorry to lecture you - I hope it helps you though.

    Regards
    Paul Lewis

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2000
    Location
    London, UK
    Posts
    32
    if i reset udtA.DONEGENERATE to 0 then the strings are generated randomly again.
    where in the code should i reset it?
    CGB

  8. #8
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Try out my previous example

    You will find that in my example, it is always the same sequence of "random" numbers each time you click the button. After you close the app and restart it, a new sequence will occur.

    Regards
    Paul Lewis

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    declare udtA.DONEGENERATE as a boolean, then if udtA.DONEGENERATE = false or true (whatever) it does not generate.

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