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.
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.
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.
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
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.
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/