Results 1 to 6 of 6

Non Repeating Random Number Generator

Threaded View

  1. #1
    Forum Contributor
    Join Date
    09-13-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    111

    Non Repeating Random Number Generator

    Hello Everyone,

    First off, thanks for looking at this post. I'm relatively new to coding in VBA and am starting to get a little bit of it. But, I have this random number generator (I didn't code it) that I've been using. Unfortunately, it's been crashing when I've been trying to modify it. I think the chance of it crashing increases as the range of random numbers decrease in proportion to the amount of numbers you want to generate increases.

    In any event, here's what I would like the code to do:
    1. List numbers 1-50
    2. List all 50 numbers random
    3. No repeats.

    Below is the code:
    Option Explicit
    
    Sub Random()
        Dim x As Long, y As Long, z As Long, tempnum As Long
        Dim flag As Boolean
        Dim i As Integer
        Dim foundCell As Range
        Application.ScreenUpdating = False
        x = Application.InputBox("Enter starting Random Number", "Random Number Generation", 1, , , , , 1)
        y = Application.InputBox("Enter ending Random Number", "Random Number Generation", 1000, , , , , 1)
        z = Application.InputBox("How many random numbers would" & "you like to generate (<15000)?", "Random Number Generation", 100, , , , , 1)
        If z = 0 Then Exit Sub
        If z > 15000 Then z = 15000
        If z > y - x + 1 Then
            MsgBox "You specified more numbers to return than " & "are possible in the range!"
            Exit Sub
        End If
        Randomize
        Cells(1, 1) = Int((y - x + 1) * Rnd + x)
        For i = 2 To z
            Do
                flag = False
                Randomize
                tempnum = Int((y - x + 1) * Rnd + x)
                Set foundCell = Range("a1", Range("a1").End(xlDown).Address).Find(tempnum)
                If Not (foundCell Is Nothing) Then
                    flag = True
                End If
            Loop Until Not flag
            Cells(i, 1) = tempnum
        Next
    End Sub
    Last edited by jeffreybrown; 11-27-2012 at 07:12 PM. Reason: Please use code tags...Thanks.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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