Any ideas how to implement Basic's RND (random numbers between

0 and 1with uniform distribution)?

Thanks

V.

## RND

### Re: RND

Hi.

There are quite a few possibilities here, though I am not sure exactly what you want.

The "standard" ODK library includes a rand() function which IIRC calls the ROM. If you need something more specific, or don't want to call the ROM routine, then there are other possibilities:

Some asm routines have already been discussed in the forums. I usually implement the random routine as:
To make it callable from C, just rename randseed to _randseed and declare the variable as extern int randseed. Also add a prototype for the function.

If you want the routine to be implemented in C, you can surely google for it.

There are quite a few possibilities here, though I am not sure exactly what you want.

The "standard" ODK library includes a rand() function which IIRC calls the ROM. If you need something more specific, or don't want to call the ROM routine, then there are other possibilities:

Some asm routines have already been discussed in the forums. I usually implement the random routine as:

Code: Select all

```
randseed
.word $dead ; will it be $dead again?
randgen
.(
lda randseed ; get old lsb of seed.
ora $308 ; lsb of VIA T2L-L/T2C-L.
rol ; this is even, but the carry fixes this.
adc $304 ; lsb of VIA TK-L/T1C-L. This is taken mod 256.
sta randseed ; random enough yet.
sbc randseed+1 ; minus the hsb of seed...
rol ; same comment than before. Carry is fairly random.
sta randseed+1 ; we are set.
rts ; see you later alligator.
.)
```

If you want the routine to be implemented in C, you can surely google for it.

### Re: RND

I don't need anything advanced. If rand() returns numbers between 0 and 1 is all I need.

Thanks

V.

Thanks

V.

- coco.oric
- Flight Lieutenant
**Posts:**434**Joined:**Tue Aug 11, 2009 9:50 am**Location:**North of France-
**Contact:**

### Re: RND

Hi, I think that Chema's routine is very random but I don't think it will provide you an uniform distribution.randgen

.(

lda randseed ; get old lsb of seed.

ora $308 ; lsb of VIA T2L-L/T2C-L.

I worked on it before, and i think you can find my posts on the forum. I'd made a routine with a call from OSDK C.

### Re: RND

Hello Coco.oric

30 years ago , to make random values for my games I used 11 eight-bits variables combinated in circle:

-First I put 1 in each value (A to K), (try other values for the random begin)

-I put in A the result of adding B and C

-I put in B the result of adding C and D

...and so on

-I put in J the result of adding K and A

-I put in K the result of adding A and B

I make an other loop.

For each loop I read the "A" value like a "random" value. (I used A and B like X and Y coordinates for some explosions effects)

Do not use less than 11 variables for a good " random effect"

30 years ago , to make random values for my games I used 11 eight-bits variables combinated in circle:

-First I put 1 in each value (A to K), (try other values for the random begin)

-I put in A the result of adding B and C

-I put in B the result of adding C and D

...and so on

-I put in J the result of adding K and A

-I put in K the result of adding A and B

I make an other loop.

For each loop I read the "A" value like a "random" value. (I used A and B like X and Y coordinates for some explosions effects)

Do not use less than 11 variables for a good " random effect"

### Re: RND

I used the following function. This function returns pseudo-random numbers that are power of 2. The argument n is used to define the upper limit of the returned values, the higher the n the lower the upper limit (e.g. for n=6 the function returns values 0, 1, 2 and 3).

int rseed=3;

int rnd(char n)

{

int rn;

rn=(52*rseed+11) % 255;

rseed=rn;

rn= rn >> n;

return rn;

}

This is not elegant neither sophisticated but it did my job.

Thanks for the help.

V.

int rseed=3;

int rnd(char n)

{

int rn;

rn=(52*rseed+11) % 255;

rseed=rn;

rn= rn >> n;

return rn;

}

This is not elegant neither sophisticated but it did my job.

Thanks for the help.

V.

### Who is online

Users browsing this forum: No registered users and 3 guests