Re: [cc65] offtopic: random numbers in c64

Date view Thread view Subject view

From: Stephan Lesch (slesch_at_studcs.uni-sb.de)
Date: 2003-06-26 16:52:54


On Thu, 26 Jun 2003, vorsichtphysiker wrote:

> Use $dc04 $dc05 instead.
> greetz carlos

Maybe it should be added that dc04/05 are a 16 bit timer which is normally
used to generate timer interrupts. It's loaded with some initial value,
and counts down to 0 (normally 1 per cycle). This method is fast and
practical if

- the timer's value range (i.e. starting value) is large enough so it
doesn't matter that some values are slightly more/less probable than the
others; either that or you set the counting range of the timer to exactly
that you want your random number to be in
- you don't have dependencies between the program and the timer
which might lead to values which are not really random.
For instance, you might run the timer, then require the user to press a
key, and afterwards, read the timer. (note that even this is not perfect,
as the keyboard is checked periodically using a timer interrupt...)

One big advantage of using the timers is the fact that you can set the
initial value to exactly the range you want the random number to be in.
So, if I have a set of 50 questions and want to choose a random one to
ask the user, I can set an unused timer to 49, let the user press a key
and just load the value of the timer.

Stephan


----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-06-26 16:56:40 CEST