|
-
Jun 29th, 2000, 12:47 AM
#1
Thread Starter
Hyperactive Member
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
-
Jun 29th, 2000, 01:15 AM
#2
Lively Member
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.
-
Jun 29th, 2000, 01:30 AM
#3
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
-
Jun 29th, 2000, 02:16 AM
#4
Thread Starter
Hyperactive Member
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.
-
Jun 29th, 2000, 02:18 AM
#5
transcendental analytic
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.
-
Jun 29th, 2000, 02:22 AM
#6
transcendental analytic
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.
-
Jun 29th, 2000, 02:25 AM
#7
Thread Starter
Hyperactive Member
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?
-
Jun 29th, 2000, 02:37 AM
#8
Hyperactive Member
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
-
Jun 29th, 2000, 03:41 AM
#9
Hyperactive Member
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]
-
Jun 29th, 2000, 05:38 AM
#10
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
-
Jun 29th, 2000, 07:05 AM
#11
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|