Commodore 128 Alive!

Commodore 128 => 128 programmers => Assembly => Topic started by: MIRKOSOFT on September 26, 2009, 12:06 PM

Title: How to generate RND number on C128 in assembler?
Post by: MIRKOSOFT on September 26, 2009, 12:06 PM
Hi!

I want to generate random numbers in assembler C128.

On C64 it is simple:
Generate routine is $E097 and 5 starting numbers are at address $E3BA with values $80/$4F/$C7/$52/$58,
everytime when is called generate routine random numbers are on adresses $8B - $8F

So, how to do it on C128?

Thanks for every reply.

Miro
Title: Re: How to generate RND number on C128 in assembler?
Post by: commodorejohn on September 26, 2009, 02:35 PM
I would just check a 6502 source site like The Fridge (http://www.ffd2.com/fridge/) for a routine; I recall reading that the BASIC random-number generator is of dubious quality.
Title: Re: How to generate RND number on C128 in assembler?
Post by: MIRKOSOFT on September 26, 2009, 03:15 PM
Hi!

I tried the same solution like C64, but routine is $8434 and numbers $121B - $121F...
...tried to do simple - really only test of random numbers, screensaver,
you can look at attachment, if you want... SYS8192

Miro
Title: Re: How to generate RND number on C128 in assembler?
Post by: LarryJor on September 27, 2009, 03:48 PM
     Well, according to "Mapping the Commodore 128", you have all the right locations.  In depth description of $121B-$121F says:
     This five-byte are holds the seed value for BASIC's random-number-generator routine ($8434).  When a positive argument is supplied, the RND routine generates the next random number by performing calculations and manipulations with the value here.  The generated values aren't really random--any given seed value here will always produce the same result.  However, the process is sufficiently complicated that the results aren't easily predictable.  Whenever any random number is generated, the resulting value is stored here for possible use as the seed for the next random number.  Location 4635/$121B is initialized to 0/$00 during the BASIC cold-start routine.  That is a change from previous Commodore models, where all five bytes of the seed value were initialized.  The zero byte has the effect of making the initial seed value 0, so the first random number value generated after the computer is turned on or after a reset will always be 1.07870447E-03.


      On an extra note, I had always read that the better way to get a truly random number was to read a value from higher in RAM; I think from the sound area, but not sure.  Hope something here helps you out.
Title: Re: How to generate RND number on C128 in assembler?
Post by: mas528 on October 07, 2009, 06:29 AM
From the "Hardware Random Number Generator" article in wikipedia:

The Commodore 64 provided a hardware random number generator based on its soundchip, the MOS Technology SID 6581. Random bytes can be fetched from memory address $D41B as long as the third oscillator's waveform is set to produce random noise.

But this will be hardware specific and there is no seed.

You can always code one for yourself.

There are very simple pseudo-random generators depending on speed and quality you want.
The linear congruential is the simplest that I know of, althought the middle-square is very easy to understand.

I think I remember that the c64 used the linear congruential method

I do not know which is the fairer method, but they are pretty good for games; not for simulations or encryption.
There is a lot of info out on the net, wikipedia is a very good start, although their categories are a mess.

Start with Random Number Generator and then Pseudo-Random Number Generator.
Title: Re: How to generate RND number on C128 in assembler?
Post by: airship on October 08, 2009, 01:24 AM
In BASIC, RND(0) takes its seed from the 6526 hardware timer used for the time of day clock values, which are held in variables TI and TI$.

A somewhat better way to seed random numbers in BASIC is to use RND(-RND(0)).

Random numbers generated using the noise generator in the SID chip should be more random than any generated by a pseudo-random number generating routine. But even those will be biased by variances in the silicon. There's no such thing as a truly random number.
Title: Re: How to generate RND number on C128 in assembler?
Post by: BigDumbDinosaur on October 09, 2009, 09:40 AM
QuoteIn BASIC, RND(0) takes its seed from the 6526 hardware timer used for the time of day clock values, which are held in variables TI and TI$.

Not quite.  RND(0) uses the value in CIA#1's TOD clock to produce a seed, but the clock output itself is  not used to generate the TI and TI$ variables (only the B128 did that).  The TI value comes from a triple precision counter that is incremented by the jiffy IRQ.

The problem with using RND(0) is that following a hard reset, the TOD clock will always contain $01 $00 $00 $00 and unless something prods the clock into action, that value will not change.  Therefore, RND(0) will continue to produce the same result unless the clock is started.
Title: Re: How to generate RND number on C128 in assembler?
Post by: Hydrophilic on October 09, 2009, 12:58 PM
BDD, I get theory of what you're saying.  But everytime I reset my C128 and type

?RND(0)

I get a different random number.  I have the buggy 1985 ROMs if that makes a difference.  I also get different numbers using VICE which has the 1986 ROMs (but of course it might be a VICE issue).

So it seems the C128 is somehow starting the TOD.  Or the code isn't using TOD.  Or maybe you are referring to another Commodore?
Title: Re: How to generate RND number on C128 in assembler?
Post by: airship on October 10, 2009, 01:13 AM
You are, of course, correct, BDD. Shortly after I posted, I got thinking that what I had written wasn't quite right, and looked it up when I got home. As soon as I read the first few words, I remembered! Duh. It sucks to get old. :(
EhPortal 1.34 © 2025, WebDev