|
-
May 14th, 2006, 01:20 PM
#1
Thread Starter
New Member
even odd random numbers
I have an application that calls a series of 75 numbers without replication. That works fine. I have a need to at times limit the sequence to either even or odd numbers that fall within the range of 1 to 75. These numbers will be referred to sequentially later so they must be written into the fields on the form in the order they are selected.
I can get the code to recognize whether the number is odd or even but it ends up writing them in the sequence they were originally drawn in on the complete 75 number set.
I would like the even number selection to write a 37 number set in theqence C1 through C37 and the odd number selection write a 38 number set in sequence C1 Through C38.
Which set is selected is will be dependant on a field that is checked to indicate which set is desired.
Any help would be appreciated.
Dim i(75) As Integer
Dim Nxt As Integer, Nxt2 As Integer
Randomize
Nxt = 1
Do Until Nxt = 76
redo:
Nxt2 = 1
i(Nxt) = Int((75 - 1 + 1) * Rnd + 1)
Do Until Nxt2 = Nxt
If i(Nxt) = i(Nxt2) Then GoTo redo
Nxt2 = Nxt2 + 1
Loop
Nxt = Nxt + 1
Loop
If Me![C1] Mod 2 = 0 Then
Exit Sub
End If
Me![C1] = i(1)
Me![C2] = i(2)
Me![C3] = i(3)
Me![C4] = i(4)
Me![C5] = i(5)
Me![C6] = i(6)
Me![C7] = i(7)
Me![C8] = i(8)
Me![C9] = i(9)
Me![C10] = i(10)
Me![C11] = i(11)
Me![C12] = i(12)
Me![C13] = i(13)
Me![C14] = i(14)
Me![C15] = i(15)
Me![C16] = i(16)
Me![C17] = i(17)
Me![C18] = i(18)
Me![C19] = i(19)
Me![C20] = i(20)
Me![C21] = i(21)
Me![C22] = i(22)
Me![C23] = i(23)
Me![C24] = i(24)
Me![C25] = i(25)
Me![C26] = i(26)
Me![C27] = i(27)
Me![C28] = i(28)
Me![C29] = i(29)
Me![C30] = i(30)
Me![C31] = i(31)
Me![C32] = i(32)
Me![C33] = i(33)
Me![C34] = i(34)
Me![C35] = i(35)
Me![C36] = i(36)
Me![C37] = i(37)
Me![C38] = i(38)
Me![C39] = i(39)
Me![C40] = i(40)
Me![C41] = i(41)
Me![C42] = i(42)
Me![C43] = i(43)
Me![C44] = i(44)
Me![C45] = i(45)
Me![C46] = i(46)
Me![C47] = i(47)
Me![C48] = i(48)
Me![C49] = i(49)
Me![C50] = i(50)
Me![C51] = i(51)
Me![C52] = i(52)
Me![C53] = i(53)
Me![C54] = i(54)
Me![C55] = i(55)
Me![C56] = i(56)
Me![C57] = i(57)
Me![C58] = i(58)
Me![C59] = i(59)
Me![C60] = i(60)
Me![C61] = i(61)
Me![C62] = i(62)
Me![C63] = i(63)
Me![C64] = i(64)
Me![C65] = i(65)
Me![C66] = i(66)
Me![C67] = i(67)
Me![C68] = i(68)
Me![C69] = i(69)
Me![C70] = i(70)
Me![C71] = i(71)
Me![C72] = i(72)
Me![C73] = i(73)
Me![C74] = i(74)
Me![C75] = i(75)
-
May 14th, 2006, 02:16 PM
#2
Re: even odd random numbers
If you want to create an array of numbers from 1 to 75 with no duplicates and ordered randomly...
- create a sorted array first
VB Code:
For x = 0 To 74
i(x) = x + 1 'shift needed to adjust zero based x
Loop
- then process this array by switching the values contained in 2 randomly selected indices.
VB Code:
Dim Idx1 As Integer
Dim Idx2 As Integer
Dim iTemp As Integer
For x = 0 To 300
'get random indices
Idx1 = Int(75 * Rnd + 1)
Do
Idx2 = Int(75 * Rnd + 1)
Loop Until Idx1 <> Idx2
'then swap i() elements
iTemp = i(Idx1)
i(Idx1) = i(Idx2)
i(Idx2) = iTemp
Next
- you can now transfer all even or all odd numbers, randomly ordered. You will simply need to use two variables... one as the counter for the For loop that will iterate the array, and the other variable incremented manually for the index in C[] at which to copy the i() array member.
-
May 14th, 2006, 02:44 PM
#3
Re: even odd random numbers
The following seems contradictory
"...a series of 75 numbers without replication. That works fine. I have a need to at times limit the sequence to either even or odd numbers that fall within the range of 1 to 75"
since there are only 37 or 38 odd or even numbers between 1 and 75.
-
May 14th, 2006, 05:43 PM
#4
Thread Starter
New Member
Re: even odd random numbers
I really don't understand this all that well. I tried your code but when I try to write the values into the C fields I get duplicates.
My problem is that contained in the 75 number set already generated are both the odd sequence I desire as well as the even number sequence I desire. I have code that cycles through the all the numbers and displays them in the sequence they were drawn but there are time when I want the code to be able to skip over either the odd or even numbers in the sequence in an efficient manner.
-
May 14th, 2006, 07:33 PM
#5
Re: even odd random numbers
VB Code:
Does this help?
Option Explicit
Private Const Odd = 1
Private Const Even = 0
Private MyCollection As New Collection
Private Sub cmdShowEven_Click()
OddOrEven Even
End Sub
Private Sub cmdShowodd_Click()
OddOrEven Odd
End Sub
Private Sub Form_Load()
Dim nTry As Integer
Dim lngIndex As Long
Randomize
'Generate the numbers 1 to 75 in random order
Do While MyCollection.Count < 75
nTry = Int(75 * Rnd + 1)
' If the number is already in the collection an error is generated
' so it is not added
On Error Resume Next
MyCollection.Add CStr(nTry), CStr(nTry)
Loop
For lngIndex = 1 To 75
List1.AddItem MyCollection(lngIndex)
Next
End Sub
Public Sub OddOrEven(intWhich As Integer)
Dim lngIndex As Long
List1.Clear
For lngIndex = 1 To 75
If MyCollection(lngIndex) Mod 2 = intWhich Then
List1.AddItem MyCollection(lngIndex)
End If
Next
End Sub
-
May 14th, 2006, 09:02 PM
#6
Thread Starter
New Member
Re: even odd random numbers
It may work but I get an error that says "Line1" not defined. I tried to define Line1 but I ddon't know where to declare it and what to declare it as.
Also, will I be able to see the numbers selected on the screen or do I need to put is controls like the C! C2..C75 to hold the even number list or the odd number list. I'm sorry I'm so obtuse on this problem For some reason this type of code simply befuddles me. I'm on some medications for cancer treatment that makes concentrating a lot more difficult than it normally is. (Please, no jokes about that being my normal state of mind)
I appreciate your efforts.
-
May 15th, 2006, 10:51 AM
#7
Re: even odd random numbers
There's no Line1 in my code. Did you perhaps hand-write the code and wrote Line1 instead of List1? My code uses a listbox to display the numbers and it may not be what you want to do. I only meant it as a demonstration/
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
|