Re: RND function on B series vs C64/C128

From: Micah Bly <micahbly_at_gmail.com>
Date: Sun, 19 Feb 2023 11:57:27 -0600
Message-Id: <758DF1AB-E92E-48BC-BA6D-69AE8AA1C15F_at_gmail.com>
Yes, that seems to be what it is doing. You can either re-seed it, or you can get out a random number from the RNG. You can’t have it return a “random” number from the system clock. 

I suspect it’s due to how the 64/128 have a “software” clock and 1/60th second jiffies, but B128 has a “hardware” clock, and only goes to 1/10th second, but that’s just supposition on my part. 

Micah


> On Feb 19, 2023, at 11:46 AM, Justin Cordesman <shadow_at_darksideresearch.com> wrote:
> 
> Sorry I meant B series.
> 
> Justin
> 
>> On Feb 19, 2023, at 11:25, Justin Cordesman <shadow_at_darksideresearch.com> wrote:
>> 
>> So does the C64/128 version consider 0 positive?
>> 
>> Justin
>> 
>>> On Feb 19, 2023, at 10:59, Micah Bly <micahbly_at_gmail.com> wrote:
>>> 
>>> Thanks for looking at that. I’ve been looking at that and the C128 source, and while you can clearly see the C128 src is branching for negative, 0, and positive, it looks like B series source is just checking negative and not-negative. Also c128 src you can see where it’s hitting the timers. But I’m not familiar with any of the BASIC sources yet, so don’t know what MOVFUM etc is. 
>>> 
>>> I’ll stick with the 700 ref manual write up in this case. 
>>> 
>>> Thanks everyone!
>>> 
>>> Micah
>>> 
>>> ——
>>> RND
>>> Summary: Return a random number
>>> Format: 
>>> RND(number)
>>> Arguments: 
>>> number is a numeric expression either less than 0 (set seed) or more than 0 (use existing seed)
>>> Abbreviation: rN
>>> 
>>> Description:
>>> 
>>> The RND function returns a pseudo-random number between 0 and 1. To use RND, feed it a positive number, and multiply that by the desired range.
>>> 
>>> For example, if you typically determine which employee will get a bonus this year by rolling a 20-sided die, use the following formula:
>>> 
>>> CHANCE=INT(RND(1)*20)+1)
>>> 
>>> RND returns a number between 0 and 1; you multiple that by the desired range (20 in this case). Now the number is greater than 0 and less than 20, so 1 is added, making the range greater than 1 and less than 21. The INT function then truncates all decimal places, leaving the result as a whole number between 1 and 20. 
>>> 
>>> You can re-seed the random number generator in the Commodore 700 by providing a negative number. It is a good idea to do this the first time you generate a random number. You do not need to do it more than once.
>>> 
>>> Note: Random numbers have many applications on the Commodore 700: You can use this in statistical analyses for your small- and medium-sized business, to randomize prospective employee interview questions, or to generate employee year end performance scores, etc.
>>> 
>>> About the Random Number Generator
>>> 
>>> RND is not actually generating random numbers: It is generating pseudo-random numbers with an algorithm called a random number generator. This algorithm essentially manages a huge list of random-looking numbers. When you ask for a random number, the next number on the list is pulled off and returned.
>>> 
>>> Every time you turn on your Commodore 700, the pointer to the random number list will be pointing to the start of it. That means that if the first thing you did every day upon turning on the computer was to ask for a random number, it would be the same number every time. The RND function provides a way to shake up the list of numbers. More accurately, it lets you select a different starting point in the list. This is called "seeding the random number generator". This does not actually make different numbers come out every time the computer is powered on. If you supply the same "seed", it will point to the same place in the list, and everyday will be groundhog day. The trick is provide something that gives it unique-ishness. A common way to do this is to seed it (once) with something that takes the system clock into account. Since you are unlikely to load and run your program in exactly the same amount of time, this effectively makes it much more random.
>>> 
>>> Examples:
>>> 
>>> Code & Result
>>> Note
>>> print rnd(0)
>>>  0.476181066
>>> Generate a number >0, <1
>>> 
>>> print int(rnd(1)*100+1)
>>>  15
>>> Generate an integer between 1 and 100
>>> 
>>> 10 x=rnd(0-val(ti$))
>>> 20 y=int(rnd(1)*6+1)
>>> Seed the random number generator based on the system clock, then generate a 6-sided dice roll. Note: When setting the seed, do not use the result.
>>> 
>>> 10 x=rnd(-67)
>>> 20 print int(rnd(1)*100+1)
>>> run
>>>  69
>>> By seeding with a fixed number, you can ensure the same numbers are generated each run of your program. Do not mention this within hearing of the Nevada Gambling Commission.
>>> 
>>> 
>>> 
>>> 
>>>> On Feb 19, 2023, at 10:41 AM, Ullrich von Bassewitz <uz_at_musoftware.de <mailto:uz_at_musoftware.de>> wrote:
>>>> 
>>>> 
>>>> This is the RND routine (grabbed from the BASIC sources):
>>>> 
>>>> ;   PSEUDO-RANDOM NUMBER GENERATOR.
>>>> RND
>>>> JSR SIGN
>>>> BMI RND1
>>>> LDX SEEDPT
>>>> LDY SEEDPT+1
>>>> INX
>>>> BNE RND10
>>>> INY
>>>> RND10
>>>> TXA
>>>> LDX #STRBNK ;ALWAYS IN STR BANK
>>>> JSR MOVFUM
>>>> LDA #<RMULC
>>>> LDY #>RMULC
>>>> JSR FMULT
>>>> LDA #<RADDC
>>>> LDY #>RADDC
>>>> JSR FADD
>>>> RND1 LDX FACLO
>>>> LDA FACHO
>>>> STA FACLO
>>>> STX FACHO
>>>> LDX FACMOH
>>>> LDA FACMO
>>>> STA FACMOH
>>>> STX FACMO
>>>> STRNEX LDA #0
>>>> STA FACSGN
>>>> LDA FACEXP
>>>> STA FACOV
>>>> LDA #$80
>>>> STA FACEXP
>>>> JSR NORMAL
>>>> LDX SEEDPT
>>>> LDY SEEDPT+1
>>>> LDA #STRBNK
>>>> INX
>>>> BNE RND20     ;IF NO CARRY
>>>> INY
>>>> RND20 JMP MOVUMF
>>>> 
>>>> If I interpret the code correctly, the description from the Reference Guide
>>>> seems to be correct.
>>>> 
>>>> Regards
>>>> 
>>>> 
>>>>        Uz
>>>> 
>>>> 
>>>> On Sun, Feb 19, 2023 at 09:39:11AM -0600, Micah Bly wrote:
>>>>> I’m working on a documentation project for the CBM II series.
>>>>> 
>>>>> In comparing the descriptions of RND() for C64, C128, and B series, and in some light testing, I think the function may behave differently. Before I write that down, I wanted to check if that made sense. I could just be misinterpreting what I’m seeing.
>>>>> 
>>>>> C128 system guide says X in RND(X) can be 0 for clock-based number, negative to pick a new see, and positive to use a random number from the RNG (not from clock).  Page 325.
>>>>> C64 wiki describes the same behavior as C128 system guide, in more detail, with a sample program to show difference between RND(0) and RND(pos).
>>>>> https://www.c64-wiki.com/wiki/RND <https://www.c64-wiki.com/wiki/RND>
>>>>> “CBM 700 Reference Guide” (the thing CBM UK wrote that Protecto distributed) says only this:
>>>>> =====
>>>>> 
>>>>> expression < 0
>>>>> 
>>>>> The algorithm uses the expression number to calculate the random number ("seed").
>>>>> 
>>>>> expression >= 0
>>>>> 
>>>>> The algorithm uses the last previously formed random number to calculate the new random number ("series").
>>>>> 
>>>>> =====
>>>>> 
>>>>> I ran the test program from the 64 wiki on the B128 (vice), and I can’t see a difference really. Certainly not the pattern you see in the pic on c64 wiki. Timer is very different on B series… is it possible they just decided not to offer that option?
>>>>> 
>>>>> Thanks,
>>>>> 
>>>>> Micah Bly
>>>>> 
>>>> 
>>>> -- 
>>>> Ullrich von Bassewitz                                  uz_at_musoftware.de <mailto:uz_at_musoftware.de>
>>>> Encrypted email preferred                          PGP Key-Id: 29D93B10
>>>> 
>>> 
Received on 2023-02-19 20:00:03

Archive generated by hypermail 2.3.0.