Results 1 to 11 of 11

Thread: randomize and Mod

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281


    Please show me how to do this. I want numbers
    between 24 and 360 to appear in my label1, at random.
    The numbers should divide perfectly (no remainder) into 360.
    For example: 24, 30, 60 etc....

    Could someone please look at my code and show me where
    I'm going wrong?

    Thanks for any help.

    Dim first As Integer
    Dim x As Integer


    Private Sub Command1_Click()
    Randomize
    first = 360 Mod x
    first = 0
    x = 24 + Int(347 * Rnd())
    Label1.Caption = x

    End Sub

  2. #2
    Lively Member
    Join Date
    May 2000
    Posts
    84
    Try something like this...

    Private Sub Command1_Click()
    Dim intArray() As Integer
    Dim i As Integer
    Dim count As Integer

    count = 0
    For i = 24 To 360
    If (360 Mod i) = 0 Then count = count + 1
    Next i

    ReDim intArray(count) As Integer

    count = 0
    For i = 24 To 360
    If (360 Mod i) = 0 Then
    count = count + 1
    intArray(count) = i
    End If
    Next i

    Randomize
    Label1.Caption = intArray(Int(count * Rnd + 1))

    End Sub

    This code creates an array of values that evenly divide into 360. Then it randomly chooses one of the values from the array.

  3. #3
    Guest
    Try this code. It will Loop our Randomization process until we find a number that divides evenly into 360.

    Code:
    Dim X As Integer
    
    REDO:
    
    ' Loop until we get a good number
    Do While Not IsWhole = True
        DoEvents
        Randomize
        X = Int(Rnd * 360)
        If X < 24 Then GoTo REDO
        DivideIt = 360 / X
        IsWhole = (DivideIt - CInt(DivideIt)) = 0
    Loop
    
    Label1.Caption = X

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Thumbs up


    Wooh! Thanks a lot, lads. I'd been doing a bit more work on it and I was going to do it along these lines:

    Private Sub Command1_Click()
    Dim dividers(0 To 7) As String
    Dim i As Integer
    'put all the factors of 360 as strings
    For i = 0 To 7
    Next
    dividers(0) = "20"
    dividers(1) = "24"
    dividers(2) = "30"
    dividers(3) = "60"
    dividers(4) = "80"
    dividers(5) = "90"
    dividers(6) = "120"
    dividers(7) = "180"
    Randomize
    i = Int(Rnd * 8)
    Label1.Caption = dividers(i)
    End Sub

    But your ways are better, neater and more to the point.

    P.S: What's the story with that damn Randomize command? Does 'Rnd * 8' mean random from 0 to 8, 1 to 8....?? It seems different every time I try it.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    And here's the faster twin:
    Code:
    Randomize Timer
    Dim a As Long
    b = 360
    Do
        a = Int(Sqr(Rnd * 360) + 1)
        If Rnd > 0.5 Then a = 360 / a
    Loop Until (360 / a) = Int(360 / a) And a = Int(a)
    Label1.Caption = a
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Randomze reinitializes your random number generator
    Rnd is a "randomly" generated number between 0 and 1
    if you use rnd*8 you will return a number between 0 and 8
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Smile


    Kedaman - Cheers,

    Megatron, you've demoralised me beyond repair. There I am thinking I'm getting the hang of VB and you throw all this crazy stuff at me: REDO, IsWhole, Cint.... Have you been doing VB since VB1 or what?

  8. #8
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    eww, GoTo.

    this is a little cleaner

    Code:
    Dim X        As Integer
    Dim IsWhole  As Boolean
    Dim DivideIt As Single
    
    IsWhole = False
    
    ' Loop until we get a good number
    Do While (Not IsWhole)
        DoEvents
        Randomize
        X = Int(Rnd * 360)
        If (X >= 24) Then
          DivideIt = 360 / X
          IsWhole = (DivideIt - CInt(DivideIt)) = 0
        End If
    Loop
    
    Label1.Caption = X
    -Shickadance

  9. #9
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    This takes fewer CPU cycles...
    Code:
    Dim v1 As Integer, v2 As Integer, v3 As Integer
    Dim v4 As Integer, v5 As Integer, v6 As Integer
    Dim res As Integer
      ' generate random whole multiples of 360 
      Do
        v1 = Rnd:    v2 = Rnd:    v3 = Rnd
        v4 = Rnd:    v5 = Rnd:    v6 = Rnd
        ' raise each prime to its power 
        res = (2 ^ v1) * (2 ^ v2) * (2 ^ v3) * (3 ^ v4) * (3 ^ v5) * (5 ^ v6)
      Loop Until res > 23
      Label1.Caption = res
    [Edited by Mongo on 06-29-2000 at 07:00 PM]

  10. #10
    Guest
    I will try to comment it a bit more and as MrShickadance said, make it cleaner.

    Code:
    ' Declare our variables
    Dim X As Integer
    Dim IsWhole As Boolean
    Dim DivideIt As Integer
    
    ' Loop until we get a good number
    Do While Not IsWhole = True
        DoEvents     ' Let Windows do other tasks
        Randomize    ' Create a Random seed value
        X = Int(Rnd * 360) 'Generate a vlue from 0-360
        If X >= 24 Then    'If X is equal to or greater than 24
            DivideIt = 360 / X    ' Store 360 / X in a variable
            
            'CInt converts the number to a whole number. So this
            'statement will subtract DivideIt from DivideIt when 
            'it is rounded as a whole number. If DivideIt has
            'some decimals, they would be left over so your 
            'answer will not equal 0, therefor, we do not accept it
            IsWhole = (DivideIt - CInt(DivideIt)) = 0
        End If
    Loop
    
    Label1.Caption = X

  11. #11
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    Otay, first stab had an admitted bias. This is better...
    Code:
    Dim res As Integer, chk As Boolean
      Do
        res = Rnd * 15
        Select Case res
          Case 1 To 6, 8 To 10, 12, 15: chk = True
          Case Else: chk = False
        End Select
      Loop Until chk
      Label1.Caption = 360 \ res

    [Edited by Mongo on 06-29-2000 at 08:07 PM]

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