Results 1 to 13 of 13

Thread: 1-15 randomize?? anyone?

  1. #1

    Thread Starter
    Hyperactive Member Abdulrahman's Avatar
    Join Date
    Oct 2000
    Location
    Scotland
    Posts
    281

    1-15 randomize?? anyone?

    Hi all,

    I have an array with 15 numbers (1 to 15)

    1 2 3 5 6 7 8 9 10 11 12 13 14 15

    how can I jumble them up randomly?? something like..

    3 2 4 12 1 15 8 10 7 4 13 11 9 5 6

    anyone?

    thanks
    Abe
    1+1=3
    make life simple, use a calculator!

  2. #2
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    259
    one way (but not the best) could be to use 2 arrays, array 1 has values... then you generate 1-15, and that value in the new array = a random 1-15 in the "real array' and then once you do that, remove it from the normal array.

    From there, you just do basic checking to see if there is a value there, so you don't get duplicates... Just reply if you want an example...
    SonicMailer Pro
    The best mailing list manager has just gotten better!
    Click here for a full list of features!

  3. #3

    Thread Starter
    Hyperactive Member Abdulrahman's Avatar
    Join Date
    Oct 2000
    Location
    Scotland
    Posts
    281

    re:

    thanks..

    how do I generate a random 1 to 15 number array?
    1+1=3
    make life simple, use a calculator!

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    VB Code:
    1. 'Each click puts one number in the box
    2. Private Sub Command1_Click()
    3. numbers
    4. End Sub
    5.  
    6. Private Sub numbers()
    7. randomize
    8. num = Int((15 * Rnd) + 1)
    9. Text1.Text = Text1.Text & num
    10. End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    259
    for an array of 15 indexes with random numbers:

    VB Code:
    1. Randomize
    2. Dim i As Integer
    3. Dim arr(1 To 15) As Integer
    4. For i = 1 To 15
    5.     arr(i) = Int(Rnd * 15) + 1
    6.     Print arr(i)
    7. Next i

    just note that you may have repeated numbers, so you'll have to add code for unique numbers.
    SonicMailer Pro
    The best mailing list manager has just gotten better!
    Click here for a full list of features!

  6. #6
    Addicted Member Mars-Martian's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    185
    there is one small problem if ur gonna use random numbers that is that what if one number appears more than once?
    I think the best way to do this then would be make a random number between 1 and 20 and if its 1 then the numbers are 4 3 6 12 5... (ones that you programmed in)
    First person to be able to get what song is currently playing in Winamp 3.

  7. #7

    Thread Starter
    Hyperactive Member Abdulrahman's Avatar
    Join Date
    Oct 2000
    Location
    Scotland
    Posts
    281
    thanks,
    i guess then there is no fast way of doing it... and hece the computer will ahve to loop many times until it gets and picks random numbers which it is yet to choose to make the the complete 15 uneque random numbers.

    coz, what I need is that, to have 15 numbers, 1 to 15, but in jumbled order.

    do you know what i mean guys?
    1+1=3
    make life simple, use a calculator!

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Try something like this

    VB Code:
    1. Dim intLoop As Integer
    2.         Dim intInner As Integer
    3.         Dim arrValues(15) As Integer
    4.         Dim arrBOolean(15) As Integer
    5.         Dim x As Integer
    6.         Dim blnVal As Boolean
    7.  
    8.         For intLoop = 0 To 14
    9.             arrBOolean(intLoop) = False
    10.         Next
    11.         For intLoop = 0 To 14
    12.             blnVal = True
    13.             While blnVal
    14.                 x = Rnd(203) * 14 + 1
    15.                 If Not arrBOolean(x) Then
    16.                     arrValues(intLoop) = x
    17.                     arrBOolean(x) = True
    18.                     blnVal = False
    19.                 End If
    20.             End While
    21.         Next
    22.  
    23.         TextBox1.Text = ""
    24.         For intLoop = 0 To 14
    25.             TextBox1.Text = TextBox1.Text & "Arrvalue(" & intLoop & ") = " & arrValues(intLoop) & vbCrLf
    26.  
    27.         Next

    Hope this helps,

  9. #9
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs up

    I use this code if that solves ur problem...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim i As Integer                    'Loop Counter
    5.     Dim usedNumbers(50) As Integer      'hold the used numbers
    6.     Dim CurrentNumber As Integer        'The current random number
    7.     Dim j As Integer                    'Loop Counter
    8.     Dim k As Integer                    'Holds our place in the UsedNumbers Array
    9.     Dim BadNumber As Boolean            'Is it a bad number?
    10.  
    11.     For i = 0 To 49
    12.         CurrentNumber = Int(Rnd() * 50) + 1
    13.         'Ensure that you get numbers between 1 and 50   Int(Rnd() * 50) + 1
    14.         For j = 0 To 49
    15.             'Check the new number against every other number we've generated
    16.             If (CurrentNumber = usedNumbers(j)) Then
    17.                 BadNumber = True
    18.                 Exit For
    19.             End If
    20.            
    21.             If (usedNumbers(j) = 0) Then
    22.                 'We will only have zeroes in the array if no value has been entered
    23.                 'in used Numbers for that index so we can safely leave if we see a zero
    24.                 Exit For
    25.             End If
    26.         Next j
    27.        
    28.         While BadNumber
    29.             'if its a bad number we need to keep generating randome numbers until we have  good one
    30.             BadNumber = False
    31.             CurrentNumber = Int(Rnd() * 50) + 1
    32.             For j = 0 To 49
    33.                 'Do the same check to see if it is a bad number
    34.                 If (CurrentNumber = usedNumbers(j)) Then
    35.                     BadNumber = True
    36.                     Exit For
    37.                 End If
    38.             Next j
    39.         Wend
    40.         'You won't get here unless you have a good number
    41.         usedNumbers(k) = CurrentNumber
    42.         k = k + 1
    43.        
    44.         Debug.Print CurrentNumber
    45.     Next i
    46. End Sub

    Cheers...

  10. #10
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Just swap the numbers, like that numbers don't repeat...
    VB Code:
    1. Public Sub RandomizeNumbers(Arr() As Integer)
    2.     Dim K As Integer
    3.     Randomize
    4.    
    5.     ' how many iterations the loop makes does not matter
    6.     ' but it should be more than UBound(Arr) to make sure all
    7.     ' numbers are swapped
    8.     For K = 0 To UBound(Arr) * 2
    9.         Swap Arr(Fix((UBound(Arr) + 1) * Rnd)), Arr(Fix((UBound(Arr) + 1) * Rnd))
    10.     Next K
    11. End Sub
    12.  
    13. Public Sub Swap(ByRef Num1 As Integer, ByRef Num2 As Integer)
    14.     Dim Tmp As Integer
    15.    
    16.     Tmp = Num1
    17.     Num1 = Num2
    18.     Num2 = Tmp
    19. End Sub

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Actually... this is better...
    VB Code:
    1. Public Sub RandomizeNumbers(Arr() As Integer)
    2.     Dim K As Integer
    3.     Randomize
    4.    
    5.     For K = 0 To UBound(Arr)
    6.         Swap Arr(K), Arr(Fix((UBound(Arr) + 1) * Rnd))
    7.     Next K
    8. End Sub
    9.  
    10. Public Sub Swap(ByRef Num1 As Integer, ByRef Num2 As Integer)
    11.     Dim Tmp As Integer
    12.    
    13.     Tmp = Num1
    14.     Num1 = Num2
    15.     Num2 = Tmp
    16. End Sub

  12. #12
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Ok, this way will be the simplest solution to your problem... interesting solutions guys...

    VB Code:
    1. 'Create initial question list - max questions
    2.     ReDim MyArray(intNumberOfElements) As Integer
    3.    
    4.     'Fill intQuestionList with initial data
    5.     Dim i As Integer
    6.     For i = 0 To (UBound(MyArray)-1)
    7.         MyArray(i) = i
    8.     Next
    9.    
    10.     'Randomize question list
    11.     Dim temp As Integer
    12.     Dim j As Integer
    13.     For i = 1 To UBound(MyArray)
    14.         Randomize
    15.         j = (Rnd(1) * (UBound(MyArray) - 1)) + 1
    16.         temp = MyArray(i)
    17.         MyArray(i) = MyArray(j)
    18.         MyArray(j) = temp
    19.     Next
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  13. #13

    Thread Starter
    Hyperactive Member Abdulrahman's Avatar
    Join Date
    Oct 2000
    Location
    Scotland
    Posts
    281

    re:

    just like to say thank you for everyone's great input to this.

    Some really very interesting ways round it too!

    thanks guys

    Abe
    1+1=3
    make life simple, use a calculator!

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