Results 1 to 10 of 10

Thread: vb6 Help generating a random number

  1. #1
    FLAC J187's Avatar
    Join Date
    Nov 2004
    Location
    Boston
    Posts
    1,237

    vb6 Help generating a random number

    I want to load a label with a random selection from a listbox with 22 items in the list. Currently I am using

    intRand = Int(Rnd * 22)
    List1.ListIndex = intRand
    List1.Selected(intRand) = True
    Label1 = List1

    the problem is, VB's RND function returns the same exact random number sequence every time the program is run - that's not very Random, is it?

    5.33424
    5.795186
    2.895625
    etc
    etc

    Anyone help me get a real Random selection??
    Its not the cards you're dealt, its how you play the hand!

    Quote Originally Posted by ryuandwings View Post
    Where can I get a roll of tin foil?
    I been looking for that all over the net, but I can't find it.
    Please help.

  2. #2
    Newbie
    Join Date
    Nov 2005
    Posts
    4
    Just write Randomize before rnd. That will solve the problem.

  3. #3
    FLAC J187's Avatar
    Join Date
    Nov 2004
    Location
    Boston
    Posts
    1,237
    Ok, if I use

    intRand = Int(randomize(Rnd * 22))
    List1.ListIndex = intRand
    List1.Selected(intRand) = True
    Label1 = List1

    which is what I think you are saying..

    I get an error - Expected expression or variable for Randomize.

    Now if do this,

    Dim intRand As Integer
    Dim strnewword As String
    Dim intBoxIndex As Integer

    intRand = Int(Rnd * 22)
    List1.ListIndex = intRand
    Randomize (intRand)
    List1.Selected(intRand) = True
    Label1 = List1

    I get a different order the words come in, but its still the same exact order everytime I load my project. Is there still something I'm doing wrong, thanks for you help..
    Its not the cards you're dealt, its how you play the hand!

    Quote Originally Posted by ryuandwings View Post
    Where can I get a roll of tin foil?
    I been looking for that all over the net, but I can't find it.
    Please help.

  4. #4
    Raw Wave Rob Withey's Avatar
    Join Date
    Apr 2000
    Location
    Bedfordshire, UK
    Posts
    2,139
    Seed the random number generator using a non deterministic source. Eg, system time.
    Old Systems retired due to new car
    New system at design/prototype stage on BeagleBoard.

  5. #5
    FLAC J187's Avatar
    Join Date
    Nov 2004
    Location
    Boston
    Posts
    1,237
    Quote Originally Posted by Rob Withey
    Seed the random number generator using a non deterministic source. Eg, system time.

    Haha, I thought of doing that, then I thought "that's Crazy". Guess not, thanks.
    Its not the cards you're dealt, its how you play the hand!

    Quote Originally Posted by ryuandwings View Post
    Where can I get a roll of tin foil?
    I been looking for that all over the net, but I can't find it.
    Please help.

  6. #6
    FLAC J187's Avatar
    Join Date
    Nov 2004
    Location
    Boston
    Posts
    1,237
    actually what I decided to do was use a timer with a 50 millisecond interval to keep things random.

    intRand = Int(Rnd * mintTimer)
    List1.ListIndex = intRand
    Label1 = List1



    Private Sub Timer1_Timer()
    'Generate random multiplier w/ timer

    If mintTimer < 20 Then
    mintTimer = mintTimer + 1
    Else
    mintTimer = 0
    End If
    End Sub

    Thank you guys very much!
    Its not the cards you're dealt, its how you play the hand!

    Quote Originally Posted by ryuandwings View Post
    Where can I get a roll of tin foil?
    I been looking for that all over the net, but I can't find it.
    Please help.

  7. #7
    Newbie
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    27
    Nah dude with the randomize he means in you code write it like this:

    Randomize

    intRand = Int(Rnd * 22)
    List1.ListIndex = intRand
    List1.Selected(intRand) = True
    Label1 = List1

  8. #8
    Raw Wave
    Join Date
    Jun 2004
    Location
    On the beach
    Posts
    2,223
    Quote Originally Posted by Rob Withey
    Seed the random number generator using a non deterministic source. Eg, system time.
    I was about to recommend that as well

  9. #9
    Constant Bitrate
    Join Date
    Mar 2006
    Posts
    217
    i believe randomize starts the sequence somewhere new based on the system clock's miliseconds or something like that. so it isnt truely random, but the number of numbers in the sequence is in like the trillians or something

  10. #10
    Variable Bitrate Splash-X's Avatar
    Join Date
    Mar 2006
    Location
    Palm Harbor, FL
    Posts
    241
    sorry to revive a dead thread but I have some insight to the problem to experience.

    When you call the Rnd functions it keeps in memory the value that was used to generate the random number. Calling Rnd again will give you the same number. Calling Randomize resets the stored value for generating the random number just like Eubey has suggested.

    If you take a look at C or C# as an example, to randomize a number in these enviroments you need to specify a seed and most programmers use the System Time to generate it. Since VB is just simplified and no built in seed method exists the Randomize functions is the way to reset the seed.

Similar Threads

  1. Remove song number in database?
    By tmar89 in forum Media Engine
    Replies: 1
    Last Post: 02-25-2005, 01:45 PM
  2. Random...Little too Random
    By Prindle19 in forum Centrafuse
    Replies: 1
    Last Post: 01-17-2005, 05:30 PM
  3. ME 2.0 Official Bug Reports
    By phat_bastard in forum ME Archive
    Replies: 67
    Last Post: 01-31-2004, 06:27 PM
  4. Error #91
    By SiGmA_X in forum ME Archive
    Replies: 18
    Last Post: 01-08-2004, 09:40 PM
  5. MPXPlay / MPXF ... random play
    By DaBretty in forum Software & Software Development
    Replies: 0
    Last Post: 07-20-2000, 09:32 PM

Bookmarks

Posting Permissions

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