Results 1 to 3 of 3

Random number generator

Threaded View

sweetjonnycrash Random number generator 10-12-2010, 02:29 PM
osusuki Re: Need help with VBA 10-12-2010, 02:45 PM
sweetjonnycrash Re: Need help with VBA 10-12-2010, 02:54 PM
  1. #1
    Registered User
    Join Date
    10-12-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    2

    Exclamation Random number generator

    I have this vba to generate random numbers 0-9..my problem is that it will generate in a column and I would like it to also generate random numbers in the row .
    any help would be greatly appreciated.

    
    Sub GetRandomNumbers()
    
    Const intLowest As Integer = 0 'Lowest number required
    
    Const intHighest As Integer = 9 'Highest number required
    
    Const intHowMany As Integer = 10 'Number of different values to return
    
    Dim booFlag As Boolean
    
    'Check to prevent infinite loop
    
    If intHowMany > (intHighest - intLowest + 1) Then
    
    MsgBox "Too many numbers or too small a range."
    
    Exit Sub
    
    End If
    
    'Set up array to hold the random value
    
    Dim arrRandom(1 To intHowMany) As Integer
    
    Randomize 'Resets random seed
    
    'Generate the first random value
    
    arrRandom(1) = Int((intHighest + 1 - intLowest) * Rnd) + intLowest
    
    'Generate subsequent random values and ensure no dupes
    
    For i = 2 To intHowMany
    
    Do
    
    booFlag = False
    
    arrRandom(i) = Int((intHighest + 1 - intLowest) * Rnd) + intLowest
    
    For j = 1 To i - 1
    
    If arrRandom(j) = arrRandom(i) Then booFlag = True
    
    Next j
    
    Loop Until booFlag = False 'Ensures value not kept if it is a dupe
    
    Next i
    
    'Write the values back to the worksheet - change "B4" reference as required
    
    For i = 1 To intHowMany
    
    Range("b4").Offset(i, 0).Value = arrRandom(i)
    
    Next i
    
    End Sub
    Last edited by sweetjonnycrash; 10-12-2010 at 04:07 PM.

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