Got a line in my game to pick random whole numbers between 1-38. I use these numbers to determine the secret spot a player must find. It says: R=INT(RND(1)*38)+1 I read about RND in the Apple II programming manual but I determined the exact code from a Google search (stack overflow method...) Problem: it's only giving me even numbers. No odd numbers. Why? And how do I fix that? ________________________________ Evan Koblentz, director Vintage Computer Federation a 501(c)(3) educational non-profit evan@vcfed.org (646) 546-9999 www.vcfed.org facebook.com/vcfederation twitter.com/vcfederation instagram.com/vcfederation
I don't know why the result is always an even number, but I suspect that this will always return the same sequence of numbers. You have to randomize the seed value of the argument to the rnd function early in the program or it will return the same sequence of numbers every time you turn the machine on.
From http://www.hoist-point.com/applesoft_basic_tutorial.htm#random_numbers_secti...
AppleSoft BASIC RND(1) function generates a pseudo-random number between 0 and 1 (excluding 1). Random numbers are used in games, computer modeling and math applications. Let's look at small program which simulates throwing a dice 20 times, generating values from 1 to 6, and prints the result. As you run this program again and again, you notice that it generates exactly the same set of numbers every time. The numbers you get from RND(1) function aren't truly random (this is why they are called pseudo-random!): computer uses an algorithm to generate them, and when program restarts, the same algorithm produces the same sequence of values. To make it generate a new sequence, you must re-initialize the random number generator by calling RND with a negative value (called random seed). Each specific value will result in different random number sequence generated afterwards. ________________________________ From: vcf-midatlantic <vcf-midatlantic-bounces@lists.vintagecomputerfederation.org> on behalf of Evan Koblentz via vcf-midatlantic <vcf-midatlantic@lists.vintagecomputerfederation.org> Sent: Friday, April 7, 2017 1:05 PM To: vcf-midatlantic Cc: Evan Koblentz Subject: [vcf-midatlantic] Random numbers Got a line in my game to pick random whole numbers between 1-38. I use these numbers to determine the secret spot a player must find. It says: R=INT(RND(1)*38)+1 I read about RND in the Apple II programming manual but I determined the exact code from a Google search (stack overflow method...) Problem: it's only giving me even numbers. No odd numbers. Why? And how do I fix that? ________________________________ Evan Koblentz, director Vintage Computer Federation a 501(c)(3) educational non-profit evan@vcfed.org (646) 546-9999 www.vcfed.org<http://www.vcfed.org> facebook.com/vcfederation twitter.com/vcfederation instagram.com/vcfederation
I don't know why the result is always an even number, but I suspect that this will always return the same sequence of numbers. You have to randomize the seed value of the argument to the rnd function early in the program or it will return the same sequence of numbers every time you turn the machine on.
From http://www.hoist-point.com/applesoft_basic_tutorial.htm#random_numbers_secti...
AppleSoft BASIC RND(1) function generates a pseudo-random number between 0 and 1 (excluding 1). Random numbers are used in games, computer modeling and math applications. Let's look at small program which simulates throwing a dice 20 times, generating values from 1 to 6, and prints the result.
As you run this program again and again, you notice that it generates exactly the same set of numbers every time. The numbers you get from RND(1) function aren't truly random (this is why they are called pseudo-random!): computer uses an algorithm to generate them, and when program restarts, the same algorithm produces the same sequence of values. To make it generate a new sequence, you must re-initialize the random number generator by calling RND with a negative value (called random seed). Each specific value will result in different random number sequence generated afterwards.
Interesting! Thanks for the link. True randomness probably doesn't matter to this program because -- yet again -- it's just for children and the set from 1-38 is big enough that most people will not notice any trends. I've to solve the even numbers mystery.
Is the random number generator running by itself or within the rest of your program? I just ran your formula in an infinite loop to print the results and I get even and odd numbers. Perhaps the other part of the program is performing a calculation that makes it even. I did it on a Commodore but it is the same code.
On Apr 7, 2017, at 1:33 PM, Evan Koblentz via vcf-midatlantic <vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
I don't know why the result is always an even number, but I suspect that this will always return the same sequence of numbers. You have to randomize the seed value of the argument to the rnd function early in the program or it will return the same sequence of numbers every time you turn the machine on.
From http://www.hoist-point.com/applesoft_basic_tutorial.htm#random_numbers_secti...
AppleSoft BASIC RND(1) function generates a pseudo-random number between 0 and 1 (excluding 1). Random numbers are used in games, computer modeling and math applications. Let's look at small program which simulates throwing a dice 20 times, generating values from 1 to 6, and prints the result.
As you run this program again and again, you notice that it generates exactly the same set of numbers every time. The numbers you get from RND(1) function aren't truly random (this is why they are called pseudo-random!): computer uses an algorithm to generate them, and when program restarts, the same algorithm produces the same sequence of values. To make it generate a new sequence, you must re-initialize the random number generator by calling RND with a negative value (called random seed). Each specific value will result in different random number sequence generated afterwards.
Interesting! Thanks for the link. True randomness probably doesn't matter to this program because -- yet again -- it's just for children and the set from 1-38 is big enough that most people will not notice any trends. I've to solve the even numbers mystery.
If you just generate a bunch of random numbers without doing anything to the number (like INT() or multiplying it) what do you get? On Fri, Apr 7, 2017 at 6:58 PM Evan Koblentz via vcf-midatlantic < vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
I don't know why the result is always an even number, but I suspect that this will always return the same sequence of numbers. You have to randomize the seed value of the argument to the rnd function early in the program or it will return the same sequence of numbers every time you turn the machine on.
From http://www.hoist-point.com/applesoft_basic_tutorial.htm#random_numbers_secti...
AppleSoft BASIC RND(1) function generates a pseudo-random number between 0 and 1 (excluding 1). Random numbers are used in games, computer modeling and math applications. Let's look at small program which simulates throwing a dice 20 times, generating values from 1 to 6, and prints the result.
As you run this program again and again, you notice that it generates exactly the same set of numbers every time. The numbers you get from RND(1) function aren't truly random (this is why they are called pseudo-random!): computer uses an algorithm to generate them, and when program restarts, the same algorithm produces the same sequence of values. To make it generate a new sequence, you must re-initialize the random number generator by calling RND with a negative value (called random seed). Each specific value will result in different random number sequence generated afterwards.
Interesting! Thanks for the link.
True randomness probably doesn't matter to this program because -- yet again -- it's just for children and the set from 1-38 is big enough that most people will not notice any trends.
I've to solve the even numbers mystery.
If you just generate a bunch of random numbers without doing anything to the number (like INT() or multiplying it) what do you get?
Applesoft generates a decimal value between 0 and 1 if you don't INT or multiply it. You can't do RND(number bigger than 1); it's a syntax error. If I remove the INT (which rounds down to .0) but keep the multiplication, then I get evens and odds. I added the INT back because I need whole numbers. But I left out the +1. Now it works as R=INT(RND(1)*38) .... thinking aloud, I have no idea why the +1 was there! :)
On Apr 7, 2017, at 11:03 PM, Evan Koblentz via vcf-midatlantic <vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
If you just generate a bunch of random numbers without doing anything to the number (like INT() or multiplying it) what do you get?
Applesoft generates a decimal value between 0 and 1 if you don't INT or multiply it. You can't do RND(number bigger than 1); it's a syntax error.
If I remove the INT (which rounds down to .0) but keep the multiplication, then I get evens and odds.
I added the INT back because I need whole numbers. But I left out the +1. Now it works as R=INT(RND(1)*38) .... thinking aloud, I have no idea why the +1 was there! :)
The +1 was there so you don't get 0. You wanted a number in the range 1-38. Without +1 it's in the range 0-37. There's no earthly reason I can think of that you'd just get even numbers with the +1 and not without -- but if you are getting both even and odd numbers without +1, you can just work around whatever it is by just adding another line right after it to increment it: 90 R=INT(RND(1)*38) 91 R=R+1 92 REM NOW R IS BETWEEN 1 AND 38 INCLUSIVE
The +1 was there so you don't get 0. You wanted a number in the range 1-38. Without +1 it's in the range 0-37.
There's no earthly reason I can think of that you'd just get even numbers with the +1 and not without -- but if you are getting both even and odd numbers without +1, you can just work around whatever it is by just adding another line right after it to increment it:
90 R=INT(RND(1)*38) 91 R=R+1 92 REM NOW R IS BETWEEN 1 AND 38 INCLUSIVE
Oh, okay. Added the 1 back. Now it works. LOL swear I'm not making this up.
I bet all numbers look even to you when staying up till the break of dawn ;) I tried that line of code and still get a wide assortment of even and odd numbers As the others in this thread explained already, you're not doing anything wrong But one small thing, it's a good practice to out the RND(-1) at the beginning of every program you want random numbers So the RND generator gets seeded again right at the start each time you run that program, so the pseudo-rnd generator still tries to remain at least pseudo vs. being predictable ;) Dan
On Sat, Apr 8, 2017 at 11:45 AM, Dan Roganti <ragooman@gmail.com> wrote:
I bet all numbers look even to you when staying up till the break of dawn ;) I tried that line of code and still get a wide assortment of even and odd numbers As the others in this thread explained already, you're not doing anything wrong
But one small thing, it's a good practice to out the RND(-1) at the beginning of every program you want random numbers So the RND generator gets seeded again right at the start each time you run that program, so the pseudo-rnd generator still tries to remain at least pseudo vs. being predictable ;) Dan
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
On Sat, Apr 8, 2017 at 11:55 AM, Dan Roganti <ragooman@gmail.com> wrote:
On Sat, Apr 8, 2017 at 11:45 AM, Dan Roganti <ragooman@gmail.com> wrote:
I bet all numbers look even to you when staying up till the break of dawn ;) I tried that line of code and still get a wide assortment of even and odd numbers As the others in this thread explained already, you're not doing anything wrong
But one small thing, it's a good practice to out the RND(-1) at the beginning of every program you want random numbers So the RND generator gets seeded again right at the start each time you run that program, so the pseudo-rnd generator still tries to remain at least pseudo vs. being predictable ;) Dan
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
This is why you like to use something "random" to seed the rnd generator Usually every computer has a free running counter, meaning it's constantly being updated in the background I don't recall at the moment where in the Apple ][ this is located, should be able to look it up again And you would PEEK this value into a variable and then use this value to Seed your RND Generator eg. 100 S=PEEK(address) * -1 110 RND(S) The seeding process requires a negative number [S< 0] which is why you multiply by -1 So then the seeding process is almost random too :) Dan
On 04/08/2017 12:10 PM, Dan Roganti via vcf-midatlantic wrote:
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
This is why you like to use something "random" to seed the rnd generator Usually every computer has a free running counter, meaning it's constantly being updated in the background
If some of my dustiest neurons are working, in the Atari at least there's a scan line counter from the video subsystem available for reading. At least I think it was something like that. That was great for seeding random number generators. The Apple ][ must have something like that. Random number generation itself is a fascinating topic and a very difficult nut to crack. There are multiple large books published about it. There are financial-industry-quality high-speed hardware random number generators on the market, used for generating crypto keys. Some are quite elaborate, using very fancy algorithms to de-bias the number stream. Some use reverse-biased P-N junctions as their initial source, and some even use tiny bits of radioactive materials coupled to scintillation crystals and photodiodes. It's a fascinating field. Generating true randomness is actually very, very hard. -Dave -- Dave McGuire, AK4HZ New Kensington, PA
On Apr 8, 2017, at 1:06 PM, Dave McGuire via vcf-midatlantic <vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
On 04/08/2017 12:10 PM, Dan Roganti via vcf-midatlantic wrote:
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
This is why you like to use something "random" to seed the rnd generator Usually every computer has a free running counter, meaning it's constantly being updated in the background
If some of my dustiest neurons are working, in the Atari at least there's a scan line counter from the video subsystem available for reading. At least I think it was something like that. That was great for seeding random number generators. The Apple ][ must have something like that.
Or just use a software “timer”, like the delay until the user does something, like move the joystick, press a key, etc. Ie, sit in a loop waiting for something to happen, incrementing a counter each time through. When the user finally does something, use the counter as a seed. Another option is to just sum up all of RAM memory since it’s unlikely exactly the same thing will be in every memory location each run. Bob
On Sat, Apr 8, 2017 at 1:16 PM, Bob Applegate via vcf-midatlantic < vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
On Apr 8, 2017, at 1:06 PM, Dave McGuire via vcf-midatlantic < vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
On 04/08/2017 12:10 PM, Dan Roganti via vcf-midatlantic wrote:
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
This is why you like to use something "random" to seed the rnd generator Usually every computer has a free running counter, meaning it's constantly being updated in the background
If some of my dustiest neurons are working, in the Atari at least there's a scan line counter from the video subsystem available for reading. At least I think it was something like that. That was great for seeding random number generators. The Apple ][ must have something like that.
Or just use a software “timer”, like the delay until the user does something, like move the joystick, press a key, etc. Ie, sit in a loop waiting for something to happen, incrementing a counter each time through. When the user finally does something, use the counter as a seed.
Another option is to just sum up all of RAM memory since it’s unlikely exactly the same thing will be in every memory location each run.
Bob
ok this is getting more weird, both McGuire's and Applegates post just arrived to my inbox and they were written & sent 9 hrs ago I'm wondering if my post at 1:14pm came thru ok the link in there has a nice writeup about this topic from a lab experiment from 1987 Dan
There are multiple large books published about it. There are financial-industry-quality high-speed hardware random number generators on the market, used for generating crypto keys. Some are quite elaborate, using very fancy algorithms to de-bias the number stream. Some use reverse-biased P-N junctions as their initial source, and some even use tiny bits of radioactive materials coupled to scintillation crystals and photodiodes. It's a fascinating field. Generating true randomness is actually very, very hard.
Simple game for kids :)
On 4/8/2017 3:27 PM, Evan Koblentz via vcf-midatlantic wrote:
There are multiple large books published about it. There are financial-industry-quality high-speed hardware random number generators on the market, used for generating crypto keys. Some are quite elaborate, using very fancy algorithms to de-bias the number stream. Some use reverse-biased P-N junctions as their initial source, and some even use tiny bits of radioactive materials coupled to scintillation crystals and photodiodes. It's a fascinating field. Generating true randomness is actually very, very hard.
Simple game for kids :)
Then there's always this solution on the other extreme end of things: https://xkcd.com/221/ -Adam
On 04/08/2017 03:27 PM, Evan Koblentz via vcf-midatlantic wrote:
There are multiple large books published about it. There are financial-industry-quality high-speed hardware random number generators on the market, used for generating crypto keys. Some are quite elaborate, using very fancy algorithms to de-bias the number stream. Some use reverse-biased P-N junctions as their initial source, and some even use tiny bits of radioactive materials coupled to scintillation crystals and photodiodes. It's a fascinating field. Generating true randomness is actually very, very hard.
Simple game for kids :)
Yes, of course. I just thought you might be interested in some background. -Dave -- Dave McGuire, AK4HZ New Kensington, PA
On 04/08/2017 03:27 PM, Evan Koblentz via vcf-midatlantic wrote:
There are multiple large books published about it. There are financial-industry-quality high-speed hardware random number generators on the market, used for generating crypto keys. Some are quite elaborate, using very fancy algorithms to de-bias the number stream. Some use reverse-biased P-N junctions as their initial source, and some even use tiny bits of radioactive materials coupled to scintillation crystals and photodiodes. It's a fascinating field. Generating true randomness is actually very, very hard.
Simple game for kids :)
Yes, of course. I just thought you might be interested in some background.
I am to an extent. Here's an article I wrote last summer: http://www.techrepublic.com/article/quantum-physics-meets-it-security/
On Apr 8, 2017, at 13:06, Dave McGuire via vcf-midatlantic <vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
On 04/08/2017 12:10 PM, Dan Roganti via vcf-midatlantic wrote:
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
This is why you like to use something "random" to seed the rnd generator Usually every computer has a free running counter, meaning it's constantly being updated in the background
If some of my dustiest neurons are working, in the Atari at least there's a scan line counter from the video subsystem available for reading. At least I think it was something like that. That was great for seeding random number generators. The Apple ][ must have something like that.
Hahaha, NOPE. The Apple II is EXTREMELY Spartan hardware. There's not even a timer in there. The only hardware you can count on is RAM, some ROM, the (0 or 1 valued) cassette interface, and some miscellaneous stuff (speaker output, paddle connector, etc.). Getting good entropy to seed is hard on an Apple II. - Dave
On Sun, Apr 9, 2017 at 6:54 AM, David Riley via vcf-midatlantic < vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
On Apr 8, 2017, at 13:06, Dave McGuire via vcf-midatlantic < vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
On 04/08/2017 12:10 PM, Dan Roganti via vcf-midatlantic wrote:
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
This is why you like to use something "random" to seed the rnd
generator
Usually every computer has a free running counter, meaning it's constantly being updated in the background
If some of my dustiest neurons are working, in the Atari at least there's a scan line counter from the video subsystem available for reading. At least I think it was something like that. That was great for seeding random number generators. The Apple ][ must have something like that.
Hahaha, NOPE. The Apple II is EXTREMELY Spartan hardware. There's not even a timer in there. The only hardware you can count on is RAM, some ROM, the (0 or 1 valued) cassette interface, and some miscellaneous stuff (speaker output, paddle connector, etc.).
Getting good entropy to seed is hard on an Apple II.
- Dave
I think we can safely deduce that none of the home computers had perfect random numbers, it was just good enough for their Basic games I'm not sure if my post came thru entirely the other day, as I'm having some issues with rcv'ing posts on time with this List, but all my other Lists work fine this was where I posted the method to use the "free-running counter" on the Apple ][ to seed the RND generator This isn't a timer, just a counter inc by the Kernal Rom in this one, On Sat, Apr 8, 2017 at 1:14 PM, Dan Roganti <ragooman@gmail.com> wrote:
ok found it, I dug up some old reference, Apple ][ has a free running counter at $4E,$4F This is a 16bit counter, so that's why it's stored in 2 memory locations ,eg. 2 bytes where $4E holds the lower byte half of the number, and $4F hold the upper byte half of the number This is used for the Keyboard Input routine in the Kernal Rom
But there's one slight glitch to work around, [on live hardware - some emulators don't replicate this entirely] I know you like to use RUN "filename" most of the times But for some reason that counter is frozen at a fixed value, basically from the time you Power On the computer. So just this first, LOAD "filename", then RUN And so the counter will be free running for as long as the computer is On
Then you just Peek those values at the beginning of your code to seed the RND And this is how you convert the Hex values into Decimal values, same as on any computer S =RND ( -1 * ( PEEK(78) + 256 * PEEK(79) ) ) http://paperity.org/p/18923593/cautions-regarding- random-number-generation-on-the-apple-ii Dan
On Sat, Apr 8, 2017 at 12:10 PM, Dan Roganti <ragooman@gmail.com> wrote:
On Sat, Apr 8, 2017 at 11:55 AM, Dan Roganti <ragooman@gmail.com> wrote:
On Sat, Apr 8, 2017 at 11:45 AM, Dan Roganti <ragooman@gmail.com> wrote:
I bet all numbers look even to you when staying up till the break of dawn ;) I tried that line of code and still get a wide assortment of even and odd numbers As the others in this thread explained already, you're not doing anything wrong
But one small thing, it's a good practice to out the RND(-1) at the beginning of every program you want random numbers So the RND generator gets seeded again right at the start each time you run that program, so the pseudo-rnd generator still tries to remain at least pseudo vs. being predictable ;) Dan
Remember, that step to seed the rnd generator is not perfect either , eg. RND(-32768) or whichever value if you use the same seed, guess what, you get the same random numbers :)
This is why you like to use something "random" to seed the rnd generator Usually every computer has a free running counter, meaning it's constantly being updated in the background I don't recall at the moment where in the Apple ][ this is located, should be able to look it up again And you would PEEK this value into a variable and then use this value to Seed your RND Generator eg. 100 S=PEEK(address) * -1 110 RND(S) The seeding process requires a negative number [S< 0] which is why you multiply by -1 So then the seeding process is almost random too :) Dan
ok found it, I dug up some old reference, Apple ][ has a free running counter at $4E,$4F This is a 16bit counter, so that's why it's stored in 2 memory locations ,eg. 2 bytes where $4E holds the lower byte half of the number, and $4F hold the upper byte half of the number This is used for the Keyboard Input routine in the Kernal Rom But there's one slight glitch to work around, [on live hardware - some emulators don't replicate this entirely] I know you like to use RUN "filename" most of the times But for some reason that counter is frozen at a fixed value, basically from the time you Power On the computer. So just this first, LOAD "filename", then RUN And so the counter will be free running for as long as the computer is On Then you just Peek those values at the beginning of your code to seed the RND And this is how you convert the Hex values into Decimal values, same as on any computer S =RND ( -1 * ( PEEK(78) + 256 * PEEK(79) ) ) http://paperity.org/p/18923593/cautions-regarding-random-number-generation-o... Dan
+1 was there to assure that INT never returns 0, as that is not in your desired range. On Sat, Apr 8, 2017 at 9:25 AM Evan Koblentz via vcf-midatlantic < vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
If you just generate a bunch of random numbers without doing anything to the number (like INT() or multiplying it) what do you get?
Applesoft generates a decimal value between 0 and 1 if you don't INT or multiply it. You can't do RND(number bigger than 1); it's a syntax error. If I remove the INT (which rounds down to .0) but keep the multiplication, then I get evens and odds. I added the INT back because I need whole numbers. But I left out the +1. Now it works as R=INT(RND(1)*38) .... thinking aloud, I have no idea why the +1 was there! :)
On Apr 7, 2017, at 1:05 PM, Evan Koblentz via vcf-midatlantic <vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
Got a line in my game to pick random whole numbers between 1-38. I use these numbers to determine the secret spot a player must find.
It says:
R=INT(RND(1)*38)+1
I read about RND in the Apple II programming manual but I determined the exact code from a Google search (stack overflow method...)
Problem: it's only giving me even numbers. No odd numbers. Why? And how do I fix that?
For what it's worth, a) it shouldn't be giving you only even numbers, and b) I failed to replicate it. FOR M=0 TO 1:PRINT INT(RND(1)*38)+1:M=0:NEXT yields plenty of both odd and even numbers for me on an emulated Apple //e (Virtual II) and a real unenhanced //e. Are you sure that's exactly the line? What computer/emulator are you running this on? -Paul
participants (11)
-
Adam Michlin -
Bob Applegate -
Chris Fala -
Dan Roganti -
Dave McGuire -
David Riley -
Dean Notarnicola -
Drew Notarnicola -
Evan Koblentz -
Hagstrom, Paul -
Kelly Leavitt