RND

Since we do not have native C compilers on the Oric, this forum will be mostly be used by people using CC65 or the OSDK. But any general C related post will be welcome !
Post Reply
vrozos
Officer Cadet
Posts: 63
Joined: Mon Nov 21, 2011 12:36 pm
Location: Athens, Greece
Contact:

RND

Post by vrozos » Wed Oct 31, 2012 9:04 am

Any ideas how to implement Basic's RND (random numbers between
0 and 1with uniform distribution)?

Thanks

V.

User avatar
Chema
Game master
Posts: 1934
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

Re: RND

Post by Chema » Wed Oct 31, 2012 9:45 am

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.

vrozos
Officer Cadet
Posts: 63
Joined: Mon Nov 21, 2011 12:36 pm
Location: Athens, Greece
Contact:

Re: RND

Post by vrozos » Wed Oct 31, 2012 1:52 pm

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

Thanks

V.

User avatar
coco.oric
Flight Lieutenant
Posts: 389
Joined: Tue Aug 11, 2009 9:50 am
Location: North of France
Contact:

Re: RND

Post by coco.oric » Wed Oct 31, 2012 2:42 pm

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.
coco.oric as DidierV, CEO Member
Image Image

User avatar
kenneth
Flying Officer
Posts: 168
Joined: Fri Nov 26, 2010 9:11 pm
Location: France PdD
Contact:

Re: RND

Post by kenneth » Fri Nov 02, 2012 10:40 pm

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"

vrozos
Officer Cadet
Posts: 63
Joined: Mon Nov 21, 2011 12:36 pm
Location: Athens, Greece
Contact:

Re: RND

Post by vrozos » Sun Nov 04, 2012 9:42 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest