+ Reply to Thread
Results 1 to 19 of 19

Random Number Help

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2004
    Posts
    9

    Random Number Help

    Hi,

    How can I make sure that I can generate random numbers that don't repeat using the rnd function in VBA please?

    Thanks

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,445

    Re: Random Number Help

    http://www.ozgrid.com/VBA/RandomNumbers.htm
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Random Number Help

    See VBE Help for Randomize.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    04-02-2004
    Posts
    9

    Re: Random Number Help

    Quote Originally Posted by sweep View Post
    Seen that thanks. I used some of the code.

    Quote Originally Posted by shg View Post
    See VBE Help for Randomize.
    Thanks for the tip. I did already look this up but it doesn't solve my problem.

    In short, I wrote a small bit of code that generates 6 lottery numbers between 1 and 49 and then it checks for a match against 6 lotto numbers I set manually. If no match then it generates another 6 numbers etc. etc. until a match is found.

    I was using the code to do some statistical analysis.

    The problem is I can demonstrate that the numbers that the vba code generate cannot be random or are random within a certain limits.

    This is because I can have 6 lotto numbers where a match is found each time, within a million tries or so and 6 lotto numbers where no match is found, even after 100 million tries, using randomize.

    Statistically, that shouldn't happen.

    Anyway, I just wanted to know if I'd done the code wrong or something.
    Last edited by rede96; 10-13-2009 at 06:17 PM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Random Number Help

    Anyway, I just wanted to know if I'd done the code wrong or something.
    Maybe (probably). Post your code.

    Excel's random number generator 2003 and later is very good.

  6. #6
    Registered User
    Join Date
    04-02-2004
    Posts
    9

    Re: Random Number Help

    Quote Originally Posted by shg View Post
    Maybe (probably). Post your code.

    Excel's random number generator 2003 and later is very good.
    Doh! Sorry about that.

    Code is below. I am using excel 2007

    Sub LottoTest()
    Randomize
    Dim CountX As Long
    Dim temp As Double
    Dim k As Long
    Dim j As Long
    Dim i As Long
    Dim l As Long
    Dim sorted As Boolean
    Dim x As Long
    Dim MyTemp As String
    Dim Arr(1 To 6) As Long
    CountX = 1 ' this variable counts how many tries it takes.
    Dim MyLotto As String
    MyLotto = "1,16,27,29,35,38" 'Test lotto numbers to match
    Dim MyView As String
    'start generating lotto numbers until a match is found or stop after 10,000,000 tries
    Do
        'generate 6 unique lotto numbers
        For i = 1 To 6
            Arr(i) = Int((49 - 1 + 1) * Rnd + 1)
                If i > 1 Then 'check for a duplicate number
    CheckDuplicate:
                    For l = 1 To i - 1
                        If Arr(i) = Arr(l) Then
                        Arr(i) = Int((49 - 1 + 1) * Rnd + 1) 'if duplicate exits, generate another number
                        GoTo CheckDuplicate 're-check for duplicate
                        End If
                    Next l
            End If
        Next i
        'some code that sorts the array
        sorted = False
        Do While Not sorted
            sorted = True
                For x = 1 To UBound(Arr) - 1
                    If Arr(x) > Arr(x + 1) Then
                        temp = Arr(x + 1)
                        Arr(x + 1) = Arr(x)
                        Arr(x) = temp
                        sorted = False
                    End If
                Next x
        Loop
        'store the 6 numbers as a string to compare against test numbers
        MyTemp = Arr(1) & "," & Arr(2) & "," & Arr(3) & "," & _
        Arr(4) & "," & Arr(5) & "," & Arr(6)
            If MyTemp = MyLotto Then 'if a match then show numbers and how many tries
                MyView = MyLotto & " took " & CountX & " tries"
                MsgBox MyView
                Exit Sub
            End If
        CountX = CountX + 1 'counts the number of tries
    Loop Until CountX = 10000000 'stops code after 10,000,000 failed attempts.
    End Sub

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Random Number Help

    Alex,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.

    Also,

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. In order to put code tags, either type [CODE] before your code and [/CODE] at the end of it, OR you can highlight your code and click the # icon at the top of your post window.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  8. #8
    Registered User
    Join Date
    06-07-2013
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Random Number Help

    Dear Arlu,

    Thank you for your information, I am a new comer...

    I did'nt posted any question, it was a solution to this thread.
    You have the code to test it and if some people have questions according to this code and what I tried to explain, they just ask me or send an email. I was just trying to help.

    Best regards,
    Alex

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1