
Dec 18th, 2002, 05:40 PM
#1
Thread Starter
Lively Member
random number formula
i heard from my algebra teacher that there is a way to write a formula that generates random numbers.. its illogical to me but if anyone knows it then please let me know.. im trying to rewrite the rnd() function in vb

Dec 18th, 2002, 06:00 PM
#2
Fanatic Member
depends on how random you get. if you are just getting values occasionally you can just get the time and extract the unit digit  but thats just integers between one to ten. however if you want to use this method to get a bunch of random numbers at the same time, you will end up with the same digit every time
Massey RuleZ! ^^__ Cheers! __^^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

Dec 18th, 2002, 06:10 PM
#3
Thread Starter
Lively Member
what would "this method" be... whats the formula

Dec 19th, 2002, 03:46 AM
#4
Fanatic Member
For functions which calculate random values a seed value is necessary. If you always use the same seed value, then you'll always get the same 'random' numbers. Because of that, the actual time (seconds after midnight, or seconds after Jan 1, 1970) is used often.

Dec 19th, 2002, 06:43 AM
#5
Frenzied Member
There are lots of random number generator algorithms  VB uses one that in a class called a pseudorandom number generator (PRNG)
From MSDN:
SUMMARY
The RND function in Visual Basic generates pseudorandom numbers according to a specific algorithm. For certain scientific or statistical studies it might be important to understand how these numbers are generated. This article documents the algorithm used.
A full treatise on the statistical nature of this algorithm is beyond the scope of this article but the topic is widely discussed in the scientific literature.
MORE INFORMATION
Microsoft Visual Basic uses the linearcongruential method for pseudorandom number generation in the RND function. The following pseudo code documents the algorithm used:
x1 = ( x0 * a + c ) MOD (2^24)
where:
x1 = new value
x0 = previous value (an initial value of 327680 is used by Visual Basic)
a = 1140671485
c = 12820163
The 'MOD' operator in the formula above returns the integer remainder after an integer division.
The expression x1/(2^24) will then return the floatingpoint number between 0.0 and 1.0 that is returned by the RND function.
Note that the above algorithm cannot be implemented in Visual Basic code in such a way that the random number sequence generated by the RND function can be reproduced. This is because internally Visual Basic uses an unsigned long data type that is not supported by the Visual Basic language.
The following C/C++ code can be used to generate the first ten pseudorandom numbers that Visual Basic generates:
#include "stdafx.h"
int main(int argc, char* argv[])
{
unsigned long rndVal;
rndVal = 0x50000L;
int i;
float rndFloat;
for (i=0;i<10;i++)
{
rndVal = (rndVal * 0x43fd43fdL + 0xc39ec3L) & 0xffffffL;
rndFloat = (float)rndVal / (float)16777216.0;
printf("Value is %.15f\n",rndFloat);
}
return 0;
}
Note that, by default, the Rnd() function will return the same sequence of pseudorandom numbers each time the program is run. For some purposes (such as statistical studies where repeatability is required) this may be appropriate. For other types of applications, such as games, this may not be appropriate. If a different sequence is required, use the Randomize statement prior to the first call to Rnd(). This will initialize the random number seed by using the system timer. If a different sequence is required but must be repeatable in future, use the syntax Randomize X where X is some specific numeric value.
It is important to recognize that Rnd() returns a new sequence for each component in which it is used; that is, if your main EXE generates one sequence and uses a Visual Basic ActiveX DLL to generate a sequence also, these sequences are independent of one another.
REFERENCES
For additional information about how earlier versions of Microsoft Basic generate pseudorandom numbers, please click the article number below to view the article in the Microsoft Knowledge Base:
28150 RND and RANDOMIZE Alternatives for Generating Random Numbers

Dec 25th, 2002, 06:43 PM
#6
Frenzied Member
Try your local library or an internet search. One size does not fit all here. My MathCad7 software provides almost 20 pseudo random number generators.
Note that random numbers is a misleading phrase. There are random processes (EG: Radioactive decay). There are sequences of numbers which pass certain statistical tests, and/or which can be used to simulate random processes. It is not exactly correct to talk about random numbers idependently of some random process.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

Dec 27th, 2002, 11:34 AM
#7
Addicted Member
Anyone can generate pseudorandom numbers using something like Microsoft used in Visual Basic. Generating perfectly random numbers is impossible, on a computer or otherwise.
Think of it in terms of a continuous random variable.
P(X = x) = 0, i.e. the probability of a randomly chosen integer is a certain integer 'x', is zero.
E(X) = 0, because the expected value of X is the mean value of all the values X can take, and since positive integers cancel out negative ones, the number left is zero.
How can you generate a random variable when you're expecting it's value to be zero?
Not at all related to sheep...

Dec 27th, 2002, 11:54 PM
#8
So Unbanned
Originally posted by A$$Bandit
Anyone can generate pseudorandom numbers using something like Microsoft used in Visual Basic. Generating perfectly random numbers is impossible, on a computer or otherwise.
Think of it in terms of a continuous random variable.
P(X = x) = 0, i.e. the probability of a randomly chosen integer is a certain integer 'x', is zero.
E(X) = 0, because the expected value of X is the mean value of all the values X can take, and since positive integers cancel out negative ones, the number left is zero.
How can you generate a random variable when you're expecting it's value to be zero?
It's just that 0 would be the technical mean of ANY(infinity to +infinity) random number.

Dec 28th, 2002, 04:56 AM
#9
transcendental analytic
Originally posted by DiGiTaIErRoR
It's just that 0 would be the technical mean of ANY(infinity to +infinity) random number.
is there any proof for this?
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p xx==0=""True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Dec 28th, 2002, 07:52 AM
#10
Fanatic Member
If you had had true randomization, and "threw 2 dice" alot, would the sum of both dice still be 7 the majority of the time?
Don't pay attention to this signature, it's contradictory.

Dec 28th, 2002, 05:33 PM
#11
Frenzied Member
Alkatran: If you mean plurality instead of majority, you are correct. A total of seven is expected to occur 1/6 of the time, which is more often than any other total when using two standard casino dice.
If a process is truly random, the data generated by it should become closer and closer to the expected values as the number of experiments grows without bound.
The VB pseudo random number generator does a fair job of simulating standard Casino Craps. An application I wrote indicates that it becomes accurate to about 35 decimal digits after a few million dice throws, but does not seem to ever get more accurate. This suggests the generator is not the best possible for this purpose. It does a worse job of simulating the overall house percentage than it does in simulating the numbers on each individual die and the totals for two dice. This indicates that the simulated sequence of numbers on each die is not quite the true sequence expected.
Kedaman: I wonder if there is a pseudo random number generator or a random process which results in a minus infinity to plus infinity range of numbers. I doubt that there is.
Given a uniform distribution over a finite range, I am pretty sure that the mean is predictable, and that this is true for many other probability distributions. The VB generator is supposed to simulate a uniform distribution of numbers between zero and one. I am not sure if it is possible for the limit value (zero/one) to occur. The expected mean is ½, which seems intuitively obvious, but might be difficult to prove. The following is a reasonable argument, but not really a proof.
By definition of a uniform distribution, every possible number has the same probability of occurring. Every group of n numbers in small range (EG: .40000 to .40002) should be matched by a similar group of n numbers symmetric to ½ (EG: .59998 to .60000). Thus, each small group of numbers is matched by another group such that the average of the two groups is ½ or close to it.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

Dec 28th, 2002, 09:48 PM
#12
So Unbanned
Originally posted by kedaman
is there any proof for this?
Yes.

Dec 28th, 2002, 09:54 PM
#13
So Unbanned
Oh you want proof huh.
Ok.
Well.
To get 'any' random number, a truly, 'random' number.
You would do something like.
0random+random. Which would be 0.

Dec 28th, 2002, 10:00 PM
#14
So Unbanned
Originally posted by DiGiTaIErRoR
Oh you want proof huh.
Ok.
Well.
To get 'any' random number, a truly, 'random' number.
You would do something like.
0random+random. Which would be 0.
Of course this is only 'perfect' if you have infinite random numbers.
i.e.
0((random*infinity)+(random*infinity))
Technically infinity minus infinity is undefined. But you get the point. As you get an ever increasing randomness you get an even increasing accuracy towards the mean of the data.
I hope.

Dec 28th, 2002, 11:45 PM
#15
So Unbanned
Dim X As Long
Dim Accuracy As Long
Dim R As Double
Accuracy = 1000000
For X = 1 To Accuracy
R = R + (Rnd * 500)
Next
MsgBox Round(R / Accuracy)
That'll always report 250.

Dec 29th, 2002, 12:10 PM
#16
transcendental analytic
DiGiTaIErRoR
I'd appreciate if you or anyone else could find or come up with one
Guv
By definition of a uniform distribution, every possible number has the same probability of occurring. Every group of n numbers in small range (EG: .40000 to .40002) should be matched by a similar group of n numbers symmetric to ½ (EG: .59998 to .60000). Thus, each small group of numbers is matched by another group such that the average of the two groups is ½ or close to it.
your reasoning could probably be backed up with a proof if you have a finite interval, however is there a definition for means for infinite intervals?
an idea passed my mind, that if you take the generalised integral for f(x)=x, you'd get lim x>+infinity f(x)  lim x>infinity f(x) = infinity  infinity, which is not defined
but if you'd take lim t>infinity integ(t to t, f(x)) then you'd end up with 0, but i think this reasoning is not valid.
lets assume that there is a pseudo random number generator that does this and that we can test with empirical data, so let it generate a number, which is as presumed, finite, n € R. What is the probability that the next number is larger than this number? What is the probability that abs(n) is smaller than abs(next number)? This would be a trivial problem if the range is finite. Lets calculate the limit with the same formula, and you'll discover that the probability that the next number is larger is 50% and that the abs(next number) is larger is 100%. For instance, let it generate a number n with finite amount of digits, it will always have infinite amount of numbers that have more digits, and only a finite number of digits that are smaller. And theres always infinite amount of numbers that are larger as well as smaller. Not a satisfactory explanation IMO, and i dont think theres a proof..
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p xx==0=""True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Dec 29th, 2002, 01:33 PM
#17
Hyperactive Member
This is the code I wrote a while ago for a linear congruential random number generator. Copy and paste into a new class module
VB Code:
'A linear congruential random number generator (LCRNG) Option Explicit ' CONSTANTS  Private Const MaxSize As Long = 2 ^ 22 '= 4194304 'used for maximum number size and 'correct fractionalization of random numbers Private Const MaxSizeTrunc As Long = MaxSize  1 '= 4194303 'used to truncate numbers to within maximum size limits ' (a mask for binary ands) Private Const MaxSizeRoot As Long = MaxSize ^ 0.5 '= 2048 'used in segmentation of large multiplications Private Const MaxSizeRootTrunc As Long = MaxSizeRoot  1 '= 2047 'used as a binary and mask in the segmentation of multiplication Private Const Multiplier As Long = 3146245 'multiplied with current random number to get new random numbers Private Const MultiplierLower As Long = Multiplier And MaxSizeRootTrunc '= 517 'precalculated lower portion of the number, used in segmentation 'of large multiplication Private Const MultiplierUpper As Long = Multiplier \ MaxSizeRoot '= 1536 'precalculated upper portion of number, same use as above Private Const AddedNum As Long = 1731 'added to multiplied numbers to generate new random numbers ' VARIABLES  Private RandomNumber As Long 'used to store the current random number Private RandomNumberLower As Long 'used to store the lower bit portion of the random number Private RandomNumberUpper As Long 'used to store the upper bit portion of the random number ' PUBLIC METHODS  Public Sub Init(ByVal Seed As Long) 'used to initialize generator RandomNumber = Seed NewRand 'used to initialize the random number generator 'the seed is set as the current random number, from which a 'new number is immediately generated End Sub Public Function Rand() As Double 'used to retrieve random numbers Rand = RandomNumber / MaxSize 'returns the current random number in decimal format, like Rnd NewRand 'generates a new random number for next use End Function ' PRIVATE METHODS  Private Sub NewRand() 'used to generate a new random number RandomNumberLower = RandomNumber And MaxSizeRootTrunc RandomNumberUpper = RandomNumber \ MaxSizeRoot 'calculates the upper and lower bit portions of the random number RandomNumber = _ ( _ AddedNum + _ ( _ ( _ ( _ ( _ RandomNumberLower * MultiplierUpper + _ MultiplierLower * RandomNumberUpper _ ) _ And MaxSizeRootTrunc _ ) _ * MaxSizeRoot + _ RandomNumberLower * MultiplierLower _ ) _ And MaxSizeTrunc _ ) _ ) _ And MaxSizeTrunc 'generates new random number using complex formula 'formatting intended to aid interpretation 'Firstly, the upper and lower portions of the current random number 'are multiplied with their opposite portions of the multiplier. 'This is truncated to the correct size to prevent overflow. 'Then, the result is multiplied by the value necessary to raise its 'position by 11 bits and make space for the actual lower bits. '(it was being worked with at lower bit positions to prevent overflow) 'This is added to the multiplication of the two lower bit portions of 'the numbers, and the result is truncated to within the actual 'limit for the numbers. (the upper bit multiplication result causes 'overflow and would only be truncated anyway) 'Finally, this is added to the constant specified in declarations, and a 'final truncation is performed to ensure that the number falls 'within the correct range. This new random number will be 'returned (in the correct format) to the used the next time 'Rand() is called. End Sub Private Sub Class_Initialize() Init Timer 'automatically initialized on startup in case user forgets to 'initialize random number generator 'has no adverse effect, nor does it prevent user from initializing 'again later End Sub
Its significantly faster than the intrinsic VB generator, and in the limited tests I've done seems to be comparably random. Beware, though, that it can only generate about 4 million unique random numbers; after that it will repeat. Whether this would cause problems depends on what its being used for.
Its easy to use; just create a new instance of the class and call class.Rand whenever you need a number between 0 and 1 (just like VB's).

Jan 1st, 2003, 04:30 PM
#18
In Javascript??
Is there a way to seed the random number generator in Javascript??

Jan 3rd, 2003, 10:35 AM
#19
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
