Results 1 to 7 of 7

Thread: even odd random numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    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)

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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:
    1. For x = 0 To 74
    2.   i(x) = x + 1  'shift needed to adjust zero based x
    3. Loop
    - then process this array by switching the values contained in 2 randomly selected indices.
    VB Code:
    1. Dim Idx1 As Integer
    2. Dim Idx2 As Integer
    3. Dim iTemp As Integer
    4.  
    5. For x = 0 To 300
    6.    'get random indices
    7.    Idx1 = Int(75 * Rnd + 1)
    8.    Do
    9.       Idx2 = Int(75 * Rnd + 1)
    10.    Loop Until Idx1 <> Idx2
    11.  
    12.    'then swap i() elements
    13.    iTemp = i(Idx1)
    14.    i(Idx1) = i(Idx2)
    15.    i(Idx2) = iTemp
    16. 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.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    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.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: even odd random numbers

    VB Code:
    1. Does this help?
    2.  
    3. Option Explicit
    4. Private Const Odd = 1
    5. Private Const Even = 0
    6. Private MyCollection As New Collection
    7.  
    8. Private Sub cmdShowEven_Click()
    9.  
    10.     OddOrEven Even
    11.    
    12. End Sub
    13.  
    14.  
    15. Private Sub cmdShowodd_Click()
    16.  
    17.     OddOrEven Odd
    18.  
    19. End Sub
    20.  
    21. Private Sub Form_Load()
    22.  
    23.     Dim nTry As Integer
    24.     Dim lngIndex As Long
    25.    
    26.     Randomize
    27.    
    28.     'Generate the numbers 1 to 75 in random order
    29.     Do While MyCollection.Count < 75
    30.         nTry = Int(75 * Rnd + 1)
    31.         ' If the number is already in the collection an error is generated
    32.         ' so it is not added
    33.         On Error Resume Next
    34.         MyCollection.Add CStr(nTry), CStr(nTry)
    35.     Loop
    36.    
    37.     For lngIndex = 1 To 75
    38.         List1.AddItem MyCollection(lngIndex)
    39.     Next
    40.    
    41. End Sub
    42.  
    43. Public Sub OddOrEven(intWhich As Integer)
    44.  
    45.     Dim lngIndex As Long
    46.  
    47.     List1.Clear
    48.     For lngIndex = 1 To 75
    49.         If MyCollection(lngIndex) Mod 2 = intWhich Then
    50.             List1.AddItem MyCollection(lngIndex)
    51.         End If
    52.     Next
    53. End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    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.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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
  •  



Click Here to Expand Forum to Full Width