473,388 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Random Number Generator

88
Hi,

I need to populate a table by generating random numbers from an array of seven values. I need to use their mean, and the standard deviation of this array, since I need the data to resemble the array as closly as possible.

I've found tons of random number generators online, but none that use the mean and standard deviation. Its been about three years since my statistics class in college and I'm drawing a blank.

Does anyone know how to do this in VB?
Jun 4 '07 #1
8 2711
fperri
88
.......or even how to see the VB code behind the random number generator in Excel?
Jun 4 '07 #2
Killer42
8,435 Expert 8TB
.......or even how to see the VB code behind the random number generator in Excel?
I don't know whether it will help, but we have a couple of snippets of VB code to generate random numbers in our VB Articles section.

Also, you might try looking up Standard Deviation on Wikipedia for info on how to calculate it. They often include samples in various programming languages. (I'd try it and let you know whether it's any good, but I'm at work now, and my employer's system blocks it.)
Jun 5 '07 #3
fperri
88
I don;t know whether it will help, but we have a couple of snippets of VB code to generate random numbers in our VB Articles section.

Also, you might try looking up Standard Deviation on Wikipedia for info on how to calculate it. They often include samples in various programming languages. (I'd try it and let you know whether it's any good, but I'm at work now, and my employer's system blocks it.)
I looked at the snid bits of code and none of them used the mean and standard dev either. Wikipedia has tons of formulas, but I'm lost at which one to use. I found an article that gave the formula that Excel supposedly uses to generate random numbers but I had to change it a bit to get it to work.....now I'm stuck.....it generates the first three random numbers fine then it reaches a point where the remainder (or Mod) is the same and it repeats itself with the same three random numbers so I don't know if I just screwed up the formula..........is there any statistics wizzes out there?

Here is the original formula that I found:

The first random number:
random_number=fractional part of (9821 * r + 0.211327), where r = .5

Successive random numbers:
random_number=fractional part of (9821 * r + 0.211327), where r = the previous random number

This formula will provide up to 1 million different numbers

I plugged the mean where the 9821 was and the standard dev into where the 0.211327 is......I'm not even completely sure this is correct but if this is the formula that Excel uses (in data analysis tools) then they must be plugged into the formula somewhere since if you want a normal distribution it prompts you for both of them........the only other thing I can think of is to try and create a macro in Excel to call the random number generator and generate a sheet for each of my samples.......I just don't want to do it this way......I want to be able to enter the sample into my database, and have a module that will populate a table from the sample data.....this would be much faster.....but I've already spent two days on this and I'm just getting more confused.......:(

Here is my code.....

Expand|Select|Wrap|Line Numbers
  1. mean = 7540209.33
  2. standardDev = 4877299.44
  3.  
  4. Set recSet = CurrentDb.OpenRecordset("TestRandomNumbers")
  5.  
  6. For i = 1 To 10000
  7.         'FIRST RANDOM NUMBER
  8.         If i = 1 Then 
  9.  
  10.             randNum = (mean * (0.5 + standardDev))
  11.             numString = Format(randNum, ".######")
  12.             'GET THE FRACTORAL PART OF THE NUMBER
  13.             numOne = Mid(numString, InStr(numString, ".") + 1)
  14.             'GET THE NUMERICAL VALUE
  15.             numTwo = Val(numOne)
  16.             'SET R TO THE FIRST NUMBER FOR SUCESSIVE RANDOM PART
  17.             r = numTwo
  18.         'SUCESSIVE RANDOM NUMBERS
  19.         Else
  20.             randNum = (mean * (r + standardDev))
  21.             numString = Format(randNum, ".######")
  22.             numOne = Mid(numString, InStr(numString, ".") + 1)
  23.             numTwo = Val(numOne)
  24.             r = numTwo
  25.         End If
  26.  
  27.         With recSet
  28.             recSet.AddNew
  29.             recSet.Fields("RandomNumber").Value = numTwo
  30.             recSet.Update
  31.         End With
  32.  
  33. Next i
  34.  
  35. End Function
Jun 5 '07 #4
fperri
88
And of course.......right after I post all that....I find something that I think will work. Cept....its in VBA4.....but I don't think that's going to be a big deal.

http://www.anthony-vba.kefra.com/vba/vba4.htm
Jun 5 '07 #5
kadghar
1,295 Expert 1GB
Hi, im not pretty sure that i understood your problem, but seems than it can be solved by using an inverse normal.

You should generate a random number between 0 and 1 and then check out what value of a Normal (yourMean, yourSDev) gives you that number.

You can make a numerical integration where you start summing areas under your Normal(yourMean, yourSDev) until the area under the curve is as big as the random number you generated between 0 and 1.

I've made up this little code:

(and for generating a list of values, just run it many times in a FOR)

Expand|Select|Wrap|Line Numbers
  1. Public Function Normal(mn As Double, sd As Double, val As Double)
  2. 'This function will help us in the numerical integration
  3.  
  4. Normal = (1 / (2.506628274631 * sd) * Exp((val - mn) ^ 2 / (-2 * sd ^ 2)))
  5.  
  6. End Function
  7. Public Function RndNumber(mean As Double, stdDev As Double)
  8.  
  9. 'First thing we have to do is to generate a random number between 0 and 1
  10. Dim x As Double
  11. x = Rnd()
  12.  
  13. 'Then we can find out using some numeric methods where our Normal integrates that value
  14. Dim boo1 As Boolean
  15. Dim y As Double
  16. Dim step As Double
  17. Dim Integration As Double
  18.  
  19. boo1 = False
  20. 'We are going to start integration from -4 std dev, and divide the range into 250 secctions
  21. y = mean + (stdDev * (-4))
  22. step = (stdDev * 8 / 250)
  23. Integration = 0
  24.  
  25. While boo1 = False
  26.     Integration = Integration + (Normal(mean, stdDev, y) + Normal(mean, stdDev, y + step)) / 2
  27.     If Integration >= x Then
  28.         boo1 = True
  29.     Else
  30.         y = y + step
  31.     End If
  32. Wend
  33.  
  34. RndNumber = (2 * y + step) / 2
  35. End Function
Good Luck
Jun 5 '07 #6
Killer42
8,435 Expert 8TB
Thanks very much for the info, kadghar.

I'd just like to point out it may be asking for trouble to use the name val for your variable or parameter, given that Val() is the name of a built-in function (possibly not in all versions).
Jun 6 '07 #7
fperri
88
Thanks kadghar,

I wish I would have checked back right away because I ended up integrating what I found on the site I linked, but your code was much shorter. I just got caught up in what I was doing. Thank you for taking the time.
Jun 19 '07 #8
kadghar
1,295 Expert 1GB
Yei!!!

Thats always a nice kind of message to read.

And you can ask me any further statistics questions , i enjoy them very much.
Jun 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.