Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Random Number + Triangular Distribution

Posted on 2000-03-24
10
819 Views
Last Modified: 2012-08-14
I need to generate a series of random numbers based on a TRIANGULAR DISTRIBUTION. Preference would be not to utilise the inbuilt Rnd function
0
Comment
Question by:jsteve
10 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2656166
what do you mean by a triangular distribution?
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2656167
Why not the Rnd function? Its statistically appropriate and built in. The only statistical fault it has is that first you have to seed it.. but.. ALL random number genrators have to do that. As such.. there is no difference between the VB Rnd and anything else on the market.. <smile>.

As to the Triangular Distribution.. if you think I'm gonna blow the dust off of my old Statistic books.. you got another think coming.. <lol>. Let's see some empirical formulae.. <smile>.
0
 
LVL 3

Expert Comment

by:fibdev
ID: 2656676
Why not

Dim N As Single
Randomize(0, N)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:MinnEE
ID: 2659142
What is the shape of the triangular distribution.? A right triangle with a positive slope? A symetrical triangle with a peak at the midpoint? or?

The way to generate your distribution is to write an equation for Y vs. X. A triangle will have to be in segments. Integerate this and get a funtion of the integral of Y vs X. Normalize the Y values so they sum to 1. Then using the rnd function, generate a number from 0 to 1. Use this as a Y value and determine the related value for X from the equation of the normalized integral of Y vs. X.
0
 
LVL 1

Expert Comment

by:MinnEE
ID: 2659454
A symterical triangular distribution that goes from X1 to X2 with a peak at (X1+X2)/2 (i.e. the midpoint) can be formed by the sum of two uniform distributions that go from X1/2 to X2/2. Each of these can be easily generated from the VB rnd function that gives a uniform distribution from 0 to 1 by multiplying and adding a constant to a variable formed by the rnd function.

Try the following code:

 Z1 = 1/2(X1 + rnd(X2-X1))
 Z2 = 1/2(X1 + rnd(X2-X1))
 Z3 = Z1 + Z2

Z3 should be a random variable with a symetrical triangular distribution from X1 to X2 with a peak at (X1 + X2)/2.

If you want a different triangular distribution, define its shape and I will try to provide some code.
0
 

Author Comment

by:jsteve
ID: 2659749
Adjusted points from 150 to 200
0
 

Author Comment

by:jsteve
ID: 2659750
Almost there. The distribution that I will be using is not symetrical. Could you provide the code. I will increase the points to 200.

Thanks
0
 
LVL 1

Expert Comment

by:MinnEE
ID: 2661814
I need the values describing the triangle. What are the 3 X values for
the begining of the triangle, the peak, and the end point. I don't need the Y values, since I will assume the triangle area corresponding to the probability must sum to one.

If you provide these I think I can give code to do this. But it will use the VB rnd function somewhere in the code.  
0
 
LVL 1

Accepted Solution

by:
MinnEE earned 200 total points
ID: 2662657
After some trial and error, I derived general equations for this. I checked the code by running 10000 variables for various shaoes and did a histogram on them.

Triangular distribution

 Note: pdf = probability density function
       cdf = cumulative probability

 Assume the pdf goes from X1 to X2 with a peak at XP. Then the cdf =
 the area under the curve of the pdf.

 Let A1 = the area from X1 to Xp
     A2 = the area from Xp to X2
 A1 + A2 = 1   (sum of all possible probs = 1)

 Writing equations for Ax as a function of X and solving for X gives
 the following code. z will be a random variable with the desired
 triangular distribution.

 A1 = (Xp-X1)/(X2-X1) ' area up to peak
 A2 = 1-A1          ' area after peak  
 r = Rnd
 If r < = A1 Then
  z = X1+Sqr(r*(X2-X1)*(Xp-X1))
 Else
  z = X2-Sqr((1-r)*(X2-Xp)*(X2-X1))
 End If
0
 
LVL 1

Expert Comment

by:MinnEE
ID: 2714649
Calculate mean and standard deviation of triangular pdf distribution.

Let a = lowest value in distribution
    b = peak of distribution
    c = largest value in distribution

Then
 Mean = (a + b + c)/3  

Let
 V = (c^3-a^3+3*b*(c^2-a^2))/(9*(c-a))  
 W = ((b-a)^3+(c-b)^3)/(18*(c-a))      

Then
 Std Dev = Sqrt(V + W - Mean^2)  
 

Derived by RJP
RJPetsch @aol.com
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question