Results 1 to 11 of 11

Thread: Need Help On a Random Function...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Question Need Help On a Random Function...

    i have this code:
    VB Code:
    1. If Check.Value = 1 Then
    2. Randomed = True
    3. Randomize List.ListCount
    4. RanVal = Int(List.ListCount * Rnd + 1)
    5. MsgBox RanVal
    6. End If

    So what it (should) do is to make a random number
    from 0 to max(list.listcount)
    and so it does, but i noticed that if i had a small amount of items in the list then it could generate numbers like: 1,5,3,7,6,7,5,6,5,3,2,3,4,8,9,8,8,4,5,2,1,2 and so on,...

    And as you see from my example, it repeats many numbers often
    what i'd like it to do is to NOT choose the same number twice before it has generated all possible numbers first...

    I mean, if i have 10 items in the list, then i want it to randomly select all of them before it starts repeating any already selected items ..like 2,4,6,8,10,1,3,5,7,9 and then it repeats...in another random order ofcourse..

    Can anyone helpp me do this??
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  2. #2
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628
    What if you just created an algorithm in an array or something that tracks all the numbers it has previously chosen, once it has chosen all of them reset the tracker.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Question I think i need an example?

    As you see, i've never worked with Random before, and arrays are neither one of my "favourites" hehe..as in, i dont know to much about arrays either...

    So any example's would give great help !!
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Talking *ding*

    Anyone?
    Anyone with an example code, or a solution?
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  5. #5
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628
    VB Code:
    1. dim x(y) as Integer
    2. dim i as Integer
    3. 'Setup(reset) array to 0 which will indicate False as in hasn't been chosen yet
    4. for i = 0 to list1.listcount-1
    5.    Redim preserve x(i+1)
    6.    x(i)=0
    7. next i
    8. 'Then whenever a random selection is done, mark the index
    9. 'in the array relative to it as 1 indicating it has been used
    10.  
    11. private sub ChooseItem_Click()
    12.    dim s as integer
    13.   'Get item selecteds index
    14.    s=list1.listindex
    15.    'Assign its index which is relative to the array the number 1
    16.    'to indicate it has been chosen.
    17.    x(s)=1
    18.  
    19. end sub
    I could elaborate more but I hope you get the gist of this.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Exclamation Didnt fully get the gist of it....

    If you could make an working example, then that would really make me learn and understand this..

    Cause the "mark the index in array..." didnt make sense to me,,,
    So if you couuld elaborate it ,then i'd be thankfull
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Ive seen several threads on this forum on the subject. Do a search for random and repeat. Here's an interesting one:

    http://www.vbforums.com/showthread.p...=random+repeat

  8. #8
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    Instead of

    Randomize List.ListCount

    try

    Randomize Timer

  9. #9
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Here is an example that works for sure cause i just wrote it from scratch and tested it.

    You need a form with 2 listboxes and 1 button, then paste all this code into your form.

    This takes the number of items in list1 and creates a random number and then adds it to list2. It continues to add random numbers to list2 untill all possible random numbers have been added.. it then repeats the process. Simply change the Counter variable number to loop as many times as you want.

    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim i As Integer, j As Integer, x As Integer, CurNum As Integer
    3.         Dim NumsUsed() As Integer, Used As Boolean, Counter As Integer
    4.         ReDim NumsUsed(0)'Initialize aray
    5.         Do
    6.         DoEvents
    7.           Do
    8.             Used = False
    9.             Randomize
    10.                 CurNum = Int((List1.ListCount) * Rnd()) + 1'get random number
    11.                   For j = 1 To UBound(NumsUsed)'loop thru random numbers allready added to see if this number has been used.
    12.                     If NumsUsed(j) = CurNum Then
    13.                         Used = True
    14.                         Exit For
    15.                     End If
    16.                 Next
    17.                 If Used = False Then 'if it hasnt been used, add it to the used list
    18.                     ReDim Preserve NumsUsed(UBound(NumsUsed) + 1)
    19.                     NumsUsed(UBound(NumsUsed)) = CurNum
    20.                 End If
    21.            Loop Until UBound(NumsUsed) = List1.ListCount ' Loop until all the numers have been added
    22.               Counter = Counter + 1 'Change this to increase the number of loops
    23.                For x = 1 To UBound(NumsUsed) 'add the numbers to list2
    24.                  list2.AddItem (NumsUsed(x))
    25.                Next
    26.               ReDim NumsUsed(0) 'reset the array to empty
    27.         Loop Until Counter = 2
    28.              
    29. End Sub
    30. Private Sub Form_Load()
    31.  Dim i As Integer
    32.         For i = 1 To 10
    33.             List1.AddItem (i)
    34.         Next
    35. End Sub
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  10. #10
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Hi Arc

    The routine works but you'll need to filter dupes
    after the scramble

    For List2

    Code:
    Private Sub Filter_Duplicates()
      Dim i As Long, J As Long
                
        With List1
            For i = 0 To .ListCount - 1
                For J = .ListCount To (i + 1) Step -1
                    If .List(J) = .List(i) Then
                        .RemoveItem J
                    End If
                Next
            Next
        End With
    End Sub
    BTW Arc.....
    That routine is just what i need for a program i'm working on!!
    Thank you...If I could send a beer via cable....

  11. #11
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I'm not sure what you mean Rocket.. there's no need to filter dupes because he wants dupes. He just doesnt want any dupes to start until all the numbers have been used.

    example
    1
    2
    3
    4
    5
    1
    2
    3
    4
    5

    Is acceptible since it uses all 5 numbers before any dupes begin
    2
    4
    3
    1
    5
    5
    4
    2
    1
    3
    is also accpetible since it uses all 5 numbers before any dupes begin

    this process continues as many times as you tell it to loop.

    But yer welcome Glad someone could use it.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


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