Jump to content
IGNORED

Do random numbers have you going in circles?


Iamgroot

Recommended Posts

This post is for those who still like to dabble in the language known as applesoft.

This is not an attempt to create the perfect random number generator, but to make the best of applesofts random generator (ARG), or when human emotions get in the way, ARGH!!!

It is said that the perfect random number generator will fill the hi-res screen to a solid white, and doesn't leave blobs of dark spots on the hi-res screen. I believe these changes makes ARG come pretty close.

These first lines will show ARG at work on regular II+ and IIe's.

1 HGR : HCOLOR= 3
5 KB = 49152: POKE 49232,0: POKE 49234,0

10 X = RND (1):Y = RND (1): HPLOT X * 280,Y * 192:I = I + 1
35 ON PEEK (KB) < 128 GOTO 10: TEXT : PRINT I: END


Add this line which changes the random number seed and run again. Any difference to applesofts seed?

15 POKE 201,128: POKE 202,79: POKE 203,199: POKE 204,82: POKE 205,88


Add these lines next and run again. Much better, No?
Line #25 adds about another 20,000 numbers to the cycle but can be omitted for speed.

20 Z = PEEK (205) + 7 - 256 * ( PEEK (205) > 248): POKE 205,Z: IF I = 6
2000 OR I = 105000 THEN T = RND (1)
25 IF I = 138500 THEN POKE 203, PEEK (203) - ( PEEK (203) > 0)


These last lines do the same as the applesoft statements above but uses an ML call to add 7 to the last digit of the random number seed to make it much faster.

9 FOR I = 0 TO 7: READ J: POKE I,J: NEXT :I = 0
20 CALL 768: IF I = 62000 OR I = 105000 THEN T = RND (1)
25 IF I = 138500 THEN POKE 203, PEEK (203) - ( PEEK (203) > 0)
50 DATA 24,165,205,105,7,133,205,96


The "I" variable can be fine tuned for about another 2000 random #'s.

Note that 2 random #'s are used at a time so the "I" variable should be doubled for the actual number of random #'s used. All-in-all the ARG cycle is almost quadrupled.

Link to comment
Share on other sites

9 FOR I = 0 TO 7: READ J: POKE I,J: NEXT :I = 0

20 CALL 768: IF I = 62000 OR I = 105000 THEN T = RND (1)

25 IF I = 138500 THEN POKE 203, PEEK (203) - ( PEEK (203) > 0)

50 DATA 24,165,205,105,7,133,205,96

 

Correction:

 

Change Line #9 to

 

9 FOR I = 0 TO 7: READ J: POKE 768+I,J: NEXT: I=0

Link to comment
Share on other sites

it is said that a true random number will leave a perfect 1 out of 8 bit pattern?

I can make a solid white screen on the II perfectly fine without random numbers giving me a expected result, aka where the hell did you hear that crap from and do you know what a true random number is? I dont dont expect a random result to give me a consistent output, I expect it to be pure garbage, hince random

 

ps peeks are ram read, pokes are ram write to the actual , um ram, it doesnt matter if its basic, lua, pascal, fartran whatever you poke a value into ram section xy, or read a ram section at xy

Edited by Osgeld
Link to comment
Share on other sites

I wonder if a small oscillator on a cheap-o expansion card could provide a white-noise like seed?

 

 

yea its called a floating joystick port, like its been done for 40 years

 

my point still stands, you can not make even a half assed random number with expected results

Edited by Osgeld
Link to comment
Share on other sites

Applesoft has a pattern, and it WILL leave blank spots if you keep doing HPLOT RND, RND. There will be numbers that never show up. If it were truly random, then eventually all numbers would show and the blank spots would eventually get filled in.

 

Many articles and newsgroup discussions have gone back and forth on techniques to avoid a repeating pattern. To no avail.

Link to comment
Share on other sites

This will basically hold true for any computer generated random number since the seed for the next random number is still calculated. Therefore if you start out with the same seed, you should always get the same sequence.

 

Also due to that calculation of the next seed, there are certain seeds which will repeat sooner. Even a white noise seed may not always be the best choice at the start, but say a new white noise seed could be introduced after so many iterations of the random number generator.

 

The seed that was picked for the test program above was hand picked for that reason. It had one of the longer cycles before repeating that I had come across.

 

The statement of randomness to fill a graphics screen to all white is only used to show how close the screen comes to getting filled in before the numbers start repeating and is still a pretty good test, maybe not so much of randomness, but of proof, of non-repeating relatively random numbers.

 

As stated in the first post, the objective was to improve applesofts RNG without introducing hefty ML code to produce perfect random numbers. The applesoft RNG is still one of the fastest out there, albeit maybe not one of the most accurate.

Link to comment
Share on other sites

Also due to that calculation of the next seed, there are certain seeds which will repeat sooner. Even a white noise seed may not always be the best choice at the start, but say a new white noise seed could be introduced after so many iterations of the random number generator.

 

Intel does that. There's a thermal noise generator in recent Core series of processors. Similar to two gates in race condition, but not quite. There's plenty of whitepapers to read about that.

 

http://spectrum.ieee.org/computing/hardware/behind-intels-new-randomnumber-generator

Link to comment
Share on other sites

yea its called a floating joystick port, like its been done for 40 years

 

I seem to remember the game port swings to one end or floats to the middle 0 - 127 - 255. I don't recall it bouncing around. I'd have to pull out my console and check it to be certain.

Edited by Keatah
Link to comment
Share on other sites

I dont dont expect a random result to give me a consistent output, I expect it to be pure garbage, hince random

 

 

Intetestingly, random number generators often have a predictable seed. It is a way to produce pseudo-random data without inputting all the values.

 

In TI BASIC, for instance, the following produces pseudo-random numbers between 1 and 10

 

10 X=INT(RND*10)+1

15 PRINT X

20 GOTO 10

 

It will give you random numbers, but the same pattern is repeated whenever you run the program. This helps when designing, say, a space shooter where you want the alien starting positions to appear random to the player, but are predictable by the author.

 

In order to counteract the predictability and generate TRULY random numbers, one can add "RANDOMIZE" to the start of the program.

 

5 RANDOMIZE

10 X=INT(RND*10)+1

15 PRINT X

20 GOTO 10

 

It is quite useful. :)

Edited by Opry99er
Link to comment
Share on other sites

It

 

In order to counteract the predictability and generate TRULY random numbers, one can add "RANDOMIZE" to the start of the program.

5 RANDOMIZE
10 X=INT(RND*10)+1
15 PRINT X
20 GOTO 10

It is quite useful. :)

 

 

It didn't give truly random numbers. All it did was give you a new starting seed and then fell back into the random number generator loop. And the generator will then calculate the next seed.

 

It is the same with the applesoft program in my first post. It increased the number of random numbers from approx 88,000 for the original applesoft seed to over 300,000. You can run this program each time and when you press a key, it will stop at a different seed each time. But this is still not true randomness as it is stopping at a different position but still on a set list of numbers.

 

RANDOMIZE does the same thing. It still uses the RNG but it uses the seed that is so-many positions away from the current seed. To see how many random #'s the RNG makes before repeating, enter this:

 

10

Link to comment
Share on other sites

It

 

 

 

It didn't give truly random numbers. All it did was give you a new starting seed and then fell back into the random number generator loop. And the generator will then calculate the next seed.

 

It is the same with the applesoft program in my first post. It increased the number of random numbers from approx 88,000 for the original applesoft seed to over 300,000. You can run this program each time and when you press a key, it will stop at a different seed each time. But this is still not true randomness as it is stopping at a different position but still on a set list of numbers.

 

RANDOMIZE does the same thing. It still uses the RNG but it uses the seed that is so-many positions away from the current seed. To see how many random #'s the RNG makes before repeating, enter this:

 

10

 

10 X = RND(1):Y = RND(1) : CT=0

20 I = RND(1): J = RND(1) : CT=CT+1

30 IF I <> X AND J <> Y GOTO 20

40 ? CT*2

 

What count do you get?

Link to comment
Share on other sites

Why do you need this? I'm not familiar with RND in Applsoft, but if you have a clock card, perhaps you could read that for the number of seconds elapsed since midnight to be your new seed, periodically changing the seed whenever a key is pressed.

 

Do you have a particular use case?

Link to comment
Share on other sites

Truly random numbers aren't evenly distributed, and they can even repeat.

But they should be unpredictable.

You start seeing patterns in a game if random numbers follow a consistent sequence.

Since that's what Microsoft BASIC does, it's important to reseed the random number generator with something unpredictable.

Clocks or user input are both good sources for a random number seed.

On the CoCo, you could use the TIMER function and user input.
Grab the timer value once the user supplies some sort of input.
Since user input is very random this works pretty well.
If you periodically repeat this, numbers coming out of the RND() function appear to be unpredictable.

The TIMER function on the CoCo is just a 16 bit counter that is bumped every top of frame interrupt.
You could also a real time clock as a source of data if it has a counter you can read that is less than a second. I think it would be more repetitive with larger intervals.

The TIMER function could be duplicated on the Apple II if you have a Mockingboard or mouse card. Something to generate an interrupt.
You could even rig up a simple circuit that generates an interrupt based on the vertical sync.
Ultimately, you need some sort of hardware with this approach.

An alternative would be to just bump a counter in your code, and when the user does something, use that counter to seed the random number generator.
It won't be as random with BASIC being slower, but it should help.

Perhaps calling RND() with a larger number based on a counter or user input would be enough to break up the sequence of the number generator.
Maybe implement a counter and perform RND(COUNTER) would suffice.
The main thing is to mess with the RND order periodically.

Edited by JamesD
Link to comment
Share on other sites

The Apple II has a 16 bit incrementing value but only while waiting for input. A key press stops the count, so requires intervention by the user to get a random number.

 

Of the Apple II line-up, only the IIGS came with a standard clock.

The IIc, IIGS and Laser 128 series have mouse ports with interrupts, but the II+ and IIe require a mouse card.

 

The IIe and up have vertical blanking but not the II+.

 

There are a lot of Apple II+ owners, so in efforts to be compatible ...

 

To be most compatible across the board of Apple computers, the routine I gave above almost quadruples the cycle of random numbers before the numbers start repeating.

Which means that given any seed, you will always get the same list of numbers from that seed. What is random is the point along that list where the next number is taken. Without intervention of some kind, the next random number will always be the next one in the list. The trick then is to start at a different position each time from that list of pre-calculated numbers using that intervention.

 

The Apple II+ is the most difficult Apple computer to get that intervention without extra hardware.

 

We could drop support for programs for the II+ to make life easier to program. But, we are not in it for the money and mischievously or maliciously drop support just to force users to upgrade.

Link to comment
Share on other sites

Truly random numbers aren't evenly distributed, and they can even repeat.

But they should be unpredictable.

You start seeing patterns in a game if random numbers follow a consistent sequence.

Since that's what Microsoft BASIC does, it's important to reseed the random number generator with something unpredictable.

 

Clocks or user input are both good sources for a random number seed.

On the CoCo, you could use the TIMER function and user input.

Grab the timer value once the user supplies some sort of input.

Since user input is very random this works pretty well.

If you periodically repeat this, numbers coming out of the RND() function appear to be unpredictable.

 

The TIMER function on the CoCo is just a 16 bit counter that is bumped every top of frame interrupt.

You could also a real time clock as a source of data if it has a counter you can read that is less than a second. I think it would be more repetitive with larger intervals.

The TIMER function could be duplicated on the Apple II if you have a Mockingboard or mouse card. Something to generate an interrupt.

You could even rig up a simple circuit that generates an interrupt based on the vertical sync.

Ultimately, you need some sort of hardware with this approach.

 

An alternative would be to just bump a counter in your code, and when the user does something, use that counter to seed the random number generator.

It won't be as random with BASIC being slower, but it should help.

Perhaps calling RND() with a larger number based on a counter or user input would be enough to break up the sequence of the number generator.

Maybe implement a counter and perform RND(COUNTER) would suffice.

The main thing is to mess with the RND order periodically.

 

If you generate random number infinitely through, the distribution should be approximately uniform though, now?

Link to comment
Share on other sites

 

If you generate random number infinitely through, the distribution should be approximately uniform though, now?

LOL, it depends on what you mean by random. This is a well argued topic elsewhere.

Whether uniform distribution is or is not truly random, it may be desirable.

In the case of a game, it could create a greater range of behavior than a theoretically perfect random number generator.

 

Link to comment
Share on other sites

LOL, it depends on what you mean by random. This is a well argued topic elsewhere.

Whether uniform distribution is or is not truly random, it may be desirable.

In the case of a game, it could create a greater range of behavior than a theoretically perfect random number generator.

 

 

By random I mean random. Each value has an equal probability of being selected, and one outcome does not in any way affect the next.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...