EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Random Number Generation

Started by ericshufro July 11, 2003
Does anyone know a way to generate psuedo random numbers in assembly
for the hc11 / hc12 / hcs12?

Thank you for the help.

--Eric



This is how its done in the Borland runtime library..... #include <stdlib.h>

#define MULTIPLIER 0x015a4e35L
#define INCREMENT 1

static long Seed = 1;

/*---------------------------------*

Name srand - initializes random number generator

Usage void srand(unsigned seed);

Prototype in stdlib.h

Description see rand below

Return value Nothing

*---------------------------------*/
void srand(unsigned seed)
{
Seed = seed;
} /*---------------------------------*

Name rand - random number generator

Usage int rand(void);

Related
functions usage void srand(unsigned seed);

Prototype in stdlib.h

Description rand uses a multiplicative congruential random number
generator with period 2^32 to return successive pseudo-
random numbers in the range from 0 to 2^15 - 1.

The generator is reinitialized by calling srand with an
argument value of 1. It can be set to a new starting point by
calling srand with a given seed number.

*---------------------------------*/
int rand(void)
{
Seed = MULTIPLIER * Seed + INCREMENT;
return((int)(Seed >> 16) & 0x7fff);
}



Yes, well Numerical Recipes in C has a whole chapter on the topic (chapter
7 to be exact). It starts off with a critique of exactly this technique.

Here are all the gory details:

http://www.ma.utexas.edu/documentation/nr/bookcpdf/c7-1.pdf

Gary Olmstead
Toucan Technology
Ventura CA

At 11:07 AM 7/11/03 -0400, you wrote:
>This is how its done in the Borland runtime library.....
>




Hi,

The old Apple II (6502) did an increment in RAM in the main program
loop. I would try the same by incrementing a 16 bit address,
so every time you read that address, the random number is between o and
65535.

Regards

Paul ericshufro wrote:

>Does anyone know a way to generate psuedo random numbers in assembly
>for the hc11 / hc12 / hcs12?
>
>Thank you for the help.
>
>--Eric >
>-------------------- >
>">http://docs.yahoo.com/info/terms/



Memfault Beyond the Launch