Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:05:54+00:00 2026-06-18T07:05:54+00:00

I want to generate random numbers from a selected distribution in VBA (Excel 2007).

  • 0

I want to generate random numbers from a selected distribution in VBA (Excel 2007).
I’m currently using the Analysis Toolpak with the following code:

     Application.Run "ATPVBAEN.XLAM!Random", "", A, B, C, D, E, F

Where

A = how many variables that are to be randomly generated 
B = number of random numbers generated per variable 
C = number corresponding to a distribution
         1= Uniform
         2= Normal
         3= Bernoulli
         4= Binomial
         5= Poisson
         6= Patterned
         7= Discrete 
D = random number seed
E = parameter of distribution (mu, lambda, etc.) depends on choice for C
(F) = additional parameter of distribution (sigma, etc.) depends on choice for C

But I want to have the random numbers be generated into an array, and NOT onto a sheet.
I understand that where the "" is designates where the random numbers should be printed to, but I don’t know the syntax for assigning the random numbers to an array, or some other form of memory storage instead of to a sheet.

I’ve tried following the syntax discussed at this Analysis Toolpak site, but have had no success.

I realize that VBA is not the ideal place to generate random numbers, but I need to do this in VBA. Any help is much appreciated! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:05:55+00:00Added an answer on June 18, 2026 at 7:05 am

    Using the inbuilt functions is the key. There is a corresponding version for each of these functions but Poisson. In my presented solution I am using an algorithm presented by Knuth to generate a random number from the Poisson Distribution.

    For Discrete or Patterned you obviously have to write your custom algorithm.

    Regarding the seed you can place a Randomize [seed] before filling your array.

    Function RandomNumber(distribution As Integer, Optional param1 = 0, Optional param2 = 0)
    
        Select Case distribution
        Case 1 'Uniform
            RandomNumber = Rnd()
        Case 2 'Normal
            RandomNumber = Application.WorksheetFunction.NormInv(Rnd(), param1, param2)
        Case 3 'Bernoulli
            RandomNumber = IIf(Rnd() > param1, 1, 0)
        Case 4 'Binomial
            RandomNumber = Application.WorksheetFunction.Binom_Inv(param1, param2, Rnd())
        Case 5 'Poisson
            RandomNumber = RandomPoisson(param1)
        Case 6 'Patterned
            RandomNumber = 0
        Case 7 'Discrete
            RandomNumber = 0
        End Select
    
    End Function
    
    Function RandomPoisson(ByVal lambda As Integer)   'Algorithm by Knuth
    
        l = Exp(-lambda)
        k = 0
        p = 1
    
        Do
             k = k + 1
             p = p * Rnd()
        Loop While p > l
    
        RandomPoisson = k - 1
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate random numbers from -n to n excluding 0. Can someone
I want to do the following dynamically Generate numbers from 1 to 100 and
I want to generate a set of non-repeating random numbers from 0...n . e.g:
I want to generate unique random, N-valued key. This key can contain numbers and
I actually want to generate random password using PHP uniqid() function $randomPassword = uniqid();
Simple enough question: I'm using python random module to generate random integers. I want
I want to generate random numbers with a range (n to m, eg 100
I want to generate eight bit (uint8_t) random numbers so that I exclude a
I want to generate a random string that has to have 5 letters from
Possible Duplicate: class System.Random .. why not static? Following on from Generated random numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.