+ Reply to Thread
Results 1 to 6 of 6

Generate Random Numbers

Hybrid View

Rgaherty Generate Random Numbers 10-30-2007, 12:57 PM
daddylonglegs Do you mean A1:F1. One way... 10-30-2007, 01:16 PM
shg The function below will enter... 10-30-2007, 01:24 PM
Rgaherty Thank you both for the input,... 10-30-2007, 02:08 PM
daddylonglegs If you don't want the numbers... 10-30-2007, 03:56 PM
shg No, just =RandSeq(), entered... 10-30-2007, 04:08 PM
  1. #1
    Registered User
    Join Date
    10-25-2007
    Posts
    85

    Generate Random Numbers

    Hey, I was wondering if there was a way to generate random numbers in a series of cells without repeating one another.

    For example, Say I want A1-D1 to be populated randomly with 1-6, not repeating a number at all. So i'd have a sequence like 132654 or 452136.

    Is that possible to do?

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697
    Do you mean A1:F1.

    One way is to use this formula in A1

    =INT(RAND()*6+1)

    and then in B1 copied across to F1

    =SMALL(IF(ISNA(MATCH({1,2,3,4,5,6},$A1:A1,0)),{1,2,3,4,5,6}),INT(RAND()*(5-COLUMN()+COLUMN($B1))+1))

    confirmed with CTRL+SHIFT+ENTER

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    The function below will enter a random sequence in whatever range it is entered. It must be entered as an array formula.

    If you don't want the sequence to change every time the sheet calculates, comment out the Application.Volatile line.

    Function RandSeq() As Variant
        ' Returns a random sequence from 1 to n to the calling range
        ' (entered in a single cell, returns 1)
        
        ' The line below will cause the sequence to recalculate whenever anything
        ' calculates. Comment it out if you only want the sequence to change
        ' only via Ctrl-Alt-F9
        Application.Volatile
        
        Dim r As Range
        Dim i As Long, j As Long, n As Long
        Dim iV As Long, nV As Long
        Dim iH As Long, nH As Long
        Dim adRand() As Double  ' random rumbers
        Dim adRank() As Integer ' rank
        
        ' verify that the caller is a range
        If TypeName(Application.Caller) <> "Range" Then Exit Function '----------->
        
        Set r = Application.Caller
        nV = r.Rows.Count
        nH = r.Columns.Count
        n = nV * nH
        
        ' size arrays to size of range
        ReDim adRand(0 To nV * nH - 1)
        ReDim adRank(0 To nV - 1, 0 To nH - 1)
        
        ' get random numbers
        Randomize
        For i = 0 To n - 1
            adRand(i) = Rnd
        Next i
        
        ' rank 'em
        For i = 0 To n - 1
            iV = i \ nH
            iH = i Mod nH
            For j = 0 To n - 1
                If adRand(i) >= adRand(j) Then adRank(iV, iH) = adRank(iV, iH) + 1
            Next
        Next
        RandSeq = adRank
    End Function

  4. #4
    Registered User
    Join Date
    10-25-2007
    Posts
    85
    Thank you both for the input, I should have mentioned that I don't want the numbers to change every time the sheet is calculated as that ends up being quite a few changes every time bogging down the system. As for the function, I saved it in the VBA editor but can't seem to call it up in the sheet itself. RandSeq(RANGE) is the proper format is it not?

  5. #5
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697
    If you don't want the numbers to change then one option is to download the free morefunc add-in and use MRAND function. This has a setting which fixes the random numbers

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Quote Originally Posted by Rgaherty
    RandSeq(RANGE) is the proper format is it not?
    No, just =RandSeq(), entered as a single array formula throuhghout the range in which you want the sequence to appear.
    Quote Originally Posted by daddylonglegs
    If you don't want the numbers to change ...
    As I said, if you don't want the numbers to change, change this
    Application.Volatile
    to
    ' Application.Volatile
    ... or just delete the line.

+ 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