Page 1 of 1
RND
Posted: Wed Oct 31, 2012 9:04 am
by vrozos
Any ideas how to implement Basic's RND (random numbers between
0 and 1with uniform distribution)?
Thanks
V.
Re: RND
Posted: Wed Oct 31, 2012 9:45 am
by Chema
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:
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.
.)
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.
Re: RND
Posted: Wed Oct 31, 2012 1:52 pm
by vrozos
I don't need anything advanced. If rand() returns numbers between 0 and 1 is all I need.
Thanks
V.
Re: RND
Posted: Wed Oct 31, 2012 2:42 pm
by coco.oric
randgen
.(
lda randseed ; get old lsb of seed.
ora $308 ; lsb of VIA T2L-L/T2C-L.
Hi, I think that Chema's routine is very random but I don't think it will provide you an uniform distribution.
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
Posted: Fri Nov 02, 2012 10:40 pm
by kenneth
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"
Re: RND
Posted: Sun Nov 04, 2012 9:42 pm
by vrozos
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.