[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/db/driver/mysqli.php on line 264: mysqli_fetch_assoc(): Couldn't fetch mysqli_result
[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/db/driver/mysqli.php on line 326: mysqli_free_result(): Couldn't fetch mysqli_result
Pete's QBASIC Site Discuss QBasic, Freebasic, QB64 and more 2005-12-05T16:57:09-05:00 http://www.petesqbsite.com/phpBB3/app.php/feed/topic/1156 2005-12-05T16:57:09-05:00 2005-12-05T16:57:09-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9857#p9857 <![CDATA[Random Numbers (are we done yet?)]]>

Statistics: Posted by Zim — Mon Dec 05, 2005 4:57 pm


]]>
2005-12-02T12:52:15-05:00 2005-12-02T12:52:15-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9753#p9753 <![CDATA[Random Numbers and Cryptography]]>
Bruce Schneirer, in his book "Applied Cryptography, 2nd Edition" describes MANY methods of generating random numbers. Other methods can be found in "Numerical Recipes" by ...what's his name... Also, Donald Knuth has a bunch of stuff on this.

My recent areas of interest here are with LFSR's (Linear Feebback Shift Registers) and Fibinocci generators which are based on the famous sequence from math class.

I have links to a few web sites that discuss randomness and genereators (as well as DOS and BASIC related sites) on my bookmark page at

http://www.chibardun.net/~zim/bookmark.htm#CryRan

I'll be back!

Statistics: Posted by Guest — Fri Dec 02, 2005 12:52 pm


]]>
2005-11-09T07:47:09-05:00 2005-11-09T07:47:09-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9290#p9290 <![CDATA[Random Numbers]]>
The chaotic function I was talking about was somewhere in the realm of

F(x) = Asinx + Bsinx

Where A/B is an irrational number.

I'm not sure if that can even be replicated on a computer (cause of the irrational number bit), but I have heard (from a mathematician, not a programmer) that the function is not periodic, so there are no patterns in the results...

It's not an iterative process (I think...), so it doesn't fall down there, but I do recognise that it's impossible to pass an X of larger then, say 64 bits, which would mean it would appear to wrap around again, but that's not the algorithms fault, it the computers :wink: ...

I don't really know what I'm talking about here, I'm just repeating what I heard cause I thought it was interesting though...
making an RNG that spits out 1 or 0 is easy,way easier than making a "proper" one that spits out.. oh.. 32bit numbers

How about just spitting out 32 1's or 0's at a time :D

matt

Statistics: Posted by matt2jones — Wed Nov 09, 2005 7:47 am


]]>
2005-11-08T20:56:19-05:00 2005-11-08T20:56:19-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9284#p9284 <![CDATA[Random Numbers]]> Statistics: Posted by Xerol — Tue Nov 08, 2005 8:56 pm


]]>
2005-11-08T20:50:13-05:00 2005-11-08T20:50:13-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9283#p9283 <![CDATA[Random Numbers]]>
a normal range thing is

x = int(rnd * x) +1 or without the +1

such as if i want a range from 0 to 200

i do

x = int(rnd * 200) or x = int(rnd *200)+1

what if i want a range of -200 to +200
how do i do that? as i dont see how to generate
a negative number in the same statement along
with a positive.....these would be coordinates
for placing objects on a map thats 400 x 400
but uses 0,0 in the center rather then the corner
of the map...

Statistics: Posted by sid6.7 — Tue Nov 08, 2005 8:50 pm


]]>
2005-11-08T19:59:53-05:00 2005-11-08T19:59:53-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9280#p9280 <![CDATA[Random Numbers]]>

Code:

x(n+1) = a*x(n)*(1-x(n))
For x(n) in [0..1] and a = [0..4].

Most notably the case for a = 4. This produces rather chaotic behavior, but the distribution of points makes it very poor for a random number generator, and also around certain values the next value is nearly identical. Here's a graph of 400 iterations:

Image

Also note for various 'a' values you get a different possible range of "final" states. The following is a graph of a = [1..4] (1 being on the left, 4 on the right) and all states of x(n) for n = [100..10000]:
Image

There's a ton of interesting crap you can do with this, but it's not really relevant. (Note that for a > 4 all values eventually diverge from [0..1] and for a < 1 they all go to 0.)

The point I'm trying to make, though, is that chaos doesn't necessarily mean random. Some functions give the illusion of randomness (as the quadratic iterator does if you just looked at a list of values of x(n)) but in reality have distinct patterns that add interesting qualities while still retaining chaotic elements.

Statistics: Posted by Xerol — Tue Nov 08, 2005 7:59 pm


]]>
2005-11-08T18:26:30-05:00 2005-11-08T18:26:30-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9266#p9266 <![CDATA[Random Numbers]]>

Code:

DECLARE FUNCTION num$ (nm&)SCREEN 12FOR mul1% = 1 TO 4096FOR mul2% = 1 TO 4096mstr$ = num$(CLNG(mul1%) * CLNG(mul2%))nstr$ = mstr$m1$ = num$(CLNG(mul1%)): m2$ = num$(CLNG(mul2%))f% = 0FOR i% = 1 TO LEN(m1$)test% = INSTR(nstr$, MID$(m1$, i%, 1))IF test% <> 0 THENMID$(nstr$, test%, 1) = SPACE$(1)f% = f% + 1ELSEEXIT FOREND IFNEXT i%FOR i% = 1 TO LEN(m2$)test% = INSTR(nstr$, MID$(m2$, i%, 1))IF test% <> 0 THENMID$(nstr$, test%, 1) = SPACE$(1)f% = f% + 1ELSEEXIT FOREND IFNEXT i%IF f% = LEN(m1$) + LEN(m2$) THEN'PRINT mstr$ + "," + m1$ + "," + m2$'SLEEPPSET (VAL(m1$) / 10 - 1, VAL(m2$) / 10 - 1), 15END IFNEXT mul2%NEXT mul1%FUNCTION num$ (nm&)num$ = RIGHT$(STR$(nm&), LEN(STR$(nm&)) - 1)END FUNCTION
Weird right? Im no big math guy, so I have little a clue as to whether or not there's a method to this. At any rate I thought this just might fit under the catgory of chaos, maybe?

Statistics: Posted by Zamaster — Tue Nov 08, 2005 6:26 pm


]]>
2005-11-08T14:16:04-05:00 2005-11-08T14:16:04-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9261#p9261 <![CDATA[Random Numbers]]>
:shock:

Of course it can return a number more then once! If you roll a dice 27 you get each number more then once, that's still a random number generator.

What I ment was there is no repitition of the pattern, eg.

int(rnd*2) -> 1 0 1 0 1 0 1 0

Where there is a pattern in the random numbers.

matt
You missed my point or missunderstood me..

A random number generator has to restart at some point.. everything has to restart, or end.. it's just a universal truth.. Nothing lasts forever :P

So a RNG would repeat at some point...

Also, making an RNG that spits out 1 or 0 is easy, way easier than making a "proper" one that spits out.. oh.. 32bit numbers


Here's a RNG I made a while ago:

Code:

function getRND (precission as uinteger = 703) as singlestatic lastnum as singledd = (((timer*1000) mod 255000)) mod 65536aa! = ddaa! = aa! mod precissionaa! = aa! / precission+lastnumwhile aa! > 1aa!=aa!-1wendlastnum = aa!getRND = aa!end function
It was just an experiment of how a RNG could look.. Also, how well it would work with a TIMER involved..

Statistics: Posted by Z!re — Tue Nov 08, 2005 2:16 pm


]]>
2005-11-08T12:25:49-05:00 2005-11-08T12:25:49-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9256#p9256 <![CDATA[Random Numbers]]>
:shock:

Of course it can return a number more then once! If you roll a dice 27 you get each number more then once, that's still a random number generator.

What I ment was there is no repitition of the pattern, eg.

int(rnd*2) -> 1 0 1 0 1 0 1 0

Where there is a pattern in the random numbers.

matt
Well, the problem is in how most random generators work. Most of them are iterated functions. For a simple, non-random one, try this:

X(n+1) = (X(n)^2 + p) mod q

(This isn't QB syntax, it's mathematical.)

Suppose P = 2 and Q = 99 and X(1) = 5. Running through this you'd get:

5
27
38
60
38
60
38
etc.

Ok, bad example there: After just 3 iterations you get into a 2-cycle. Let's try p = 7 and q = 101, and x(1) = 5 again:

5
32
21
44
24
88
75
77
78
31
59
54
and so on. (I could keep going, but I didn't feel like writing a program to do it and I'm just doing them by hand.) But eventually you'd hit one of those numbers again, which would start a cycle, since x(n+1) is always going to be the same given x(n). Now, you could use different P, Q, and X(1) values, which all have their own cycles and oddities (as 2, 99, 5 did above). You can make Q high enough, and with Q prime can end up with some fairly long non-repeating cycles. But as soon as you repeat a number, you're in a cycle.

One way to avoid this is to make the new number dependant on the previous TWO numbers, as such:

x(n+1) = (x(n)*(x(n-1)^2) + p) mod q

Thus the conditions required to intiate a cycle are much more difficult to achieve, but could happen eventually. (This time, you'd get into a cycle as soon as any two-number sequence repeats.)

Real generators use a more complex function than just squaring the number, but I just used that for simplicity. With two numbers to work with(in addition to P and Q), we can also do some more interesting things. Let's try for p = 11, q = 37, x(0) = 1, and x(1) = 27:

(1)
(27)
2
26
4
25
15
22
36
13
25 (GASP! A repeat!)
20 (But the next number is different!)
27 (Another repeat)
31 (But things are different again)
31
4

Also note that 31 repeated there. If a number repeated in the one-previous equation, it would just keep repeating (a 1-cycle). The problem is, you'd probably eventually hit a two-number cycle again. Solutions to this are to either continually increase the number of previous terms used (which require more seeds and more computation), or to simply use a Q value high enough that any cycle would be millions or billions of numbers long, or a combination of both.

The modulus arithmetic used in random number generators makes it difficult to reverse-engineer an equation just by looking at the random numbers generated for a given seed. Note in C and other languages, the random number function returns an integer, not a [0..1] float, which you then can MOD by whatever range of random numbers you need.

Also note that P and Q aren't "seeds", they're parameters in the function. The only seed(s) are pre-set X(n) values used before the first number is generated.


EDIT: Ok, I wrote a small program. First the code:

Code:

dim as ulongint p, q, x0, x1, xn, numprint "Random Number Test Generator by Xerol"print "Using equation x(n) = ((x(n-1)*(x(n-2)^2) + p) mod q"input "Value to use for p:", pinput "Value to use for q:", qinput "Seed #1 (x(0)):", x0input "Seed #2 (x(1)):", x1input "Number of numbers to generate:", numopen "random.txt" for output as #1print #1, "P = "; pprint #1, "Q = "; qprint #1, "x0 = "; x0print #1, "x1 = "; x1print #1, "Generating "; num; " numbers using above parameters."for n = 1 to num    xn = (x0*(x1^2)+p) mod q    print xn    print #1, xn    x0 = x1    x1 = xnnext nsleep
(This is for FreeBasic. A ulongint is a 64-bit unsigned integer, so you can use VERY high numbers. Use 2^64-1 as Q (whatever that is) and divide by 2^64 to get numbers in a [0..1] range like QB.)

Now the output:

Code:

P = 101Q = 2538213x0 = 300295x1 = 1828216Generating 500 numbers using above parameters.2395437249504858439371752621381103940085824793123179328099545981958796(List truncated for sake of space. I'll upload the file later.)
Not sure if there's any repeats in that or not. A quick scan shows 500 seemingly random numbers, though.

One good test of how well a random number generator works is to show a distribution of the numbers it outputs. So, we'll do a histogram. I divided the interval of [0..q] into 800 parts, and plotted a histogram:

Image
500 numbers isn't a good indicator of how well a generator does. Let's try 500000 points:
Image
That doesn't look too good. There's obvious peaks and valleys. Maybe it's just the seeds. Let's try with x0 = 29392:
Image
The distribution is nearly identical. So this equation isn't that great. Generally, though, you want to keep the (crap + p) mod q format. Crap just needs to be different.


(I think this is a pretty good start on a Random Number Generation article for QBXP. I might just write it.)

Ok, I just tried different numbers, same equation. Got slightly better results:

Image

This is with:
p = 101
q = 2530887
x0 = 2939212
x1 = 1821623
For 500,000 iterations. The choice of Q seems to be the driving factor here. So maybe it isn't such a bad formula after all.

Statistics: Posted by Xerol — Tue Nov 08, 2005 12:25 pm


]]>
2005-11-08T12:19:00-05:00 2005-11-08T12:19:00-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9255#p9255 <![CDATA[Random Numbers]]> Statistics: Posted by Zamaster — Tue Nov 08, 2005 12:19 pm


]]>
2005-11-08T10:58:44-05:00 2005-11-08T10:58:44-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9252#p9252 <![CDATA[Random Numbers]]>

Of course it can return a number more then once! If you roll a dice 27 you get each number more then once, that's still a random number generator.

What I ment was there is no repitition of the pattern, eg.

int(rnd*2) -> 1 0 1 0 1 0 1 0

Where there is a pattern in the random numbers.

matt

Statistics: Posted by matt2jones — Tue Nov 08, 2005 10:58 am


]]>
2005-11-08T10:39:46-05:00 2005-11-08T10:39:46-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9248#p9248 <![CDATA[Random Numbers]]>
All RND generators wrap around at some point, often far sooner than you might think..
The Chaotic Functions I was talking about don't, that's why I brought them up... Non-periodic or something...

matt
That is impossible, if you have a finite amount of numbers, you will wrap around and repeat at some point..

Simple example to prove the point:
Assume you have a 1bit number, that is: 0 to 1 can be represented..

You can do the following combinations:
10, 11, 01 and 00

You can combine them in different ways etc.. but they will rpeeat after a while..

As you can see even a 1bit number will create a huge table before it wraps around..
But with todays computers, even a 32bit number is only a matter of minutes, if not seconds, before it wraps around..

Statistics: Posted by Z!re — Tue Nov 08, 2005 10:39 am


]]>
2005-11-08T08:05:26-05:00 2005-11-08T08:05:26-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9244#p9244 <![CDATA[Random Numbers]]>
All RND generators wrap around at some point, often far sooner than you might think..
The Chaotic Functions I was talking about don't, that's why I brought them up... Non-periodic or something...

matt

Statistics: Posted by matt2jones — Tue Nov 08, 2005 8:05 am


]]>
2005-11-08T07:25:19-05:00 2005-11-08T07:25:19-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9241#p9241 <![CDATA[Re: Random Numbers]]>
Can anyone clue me in to the workings of random number generators?
using search machine and the words Pseudo-random Numbers you will find some formulas but seeing that we are in Quickbasics you can find the formula used by the Basics of Microsoft into this article of the Quickbasics knowledge base.
http://support.microsoft.com/default.as ... n-us;28150

Statistics: Posted by Macric — Tue Nov 08, 2005 7:25 am


]]>
2005-11-08T07:01:02-05:00 2005-11-08T07:01:02-05:00 http://www.petesqbsite.com/phpBB3/viewtopic.php?p=9240#p9240 <![CDATA[Random Numbers]]> (In QB you can pass this as an argument)

It then stores that value in a variable, divide by a number, multiplicate and such..

And spits out a new number..

The new number is also stored..


Next time you call the function it does the same thing.. the "new number" is moved to the "old number" and a real new number is created and returned using the "old number" and "new number"


All RND generators wrap around at some point, often far sooner than you might think..

Statistics: Posted by Z!re — Tue Nov 08, 2005 7:01 am


]]>