Jump to content
Sign in to follow this  
RSABear

Boostc - Rand() And Srand()

Recommended Posts

Hi to all on the Forum,

 

The BoostC compiler supports two miscellaneous functions, unsigned short rand() and srand() from the library rand.lib. I would like to know if anybody has implemented these functions and have some guidelines for me to use them.

 

How would you do a dice 1 – 6 (0x00 – 0x06) or 1 to 16 (0x00 – 0x0F), maybe even 0x00 to 0xFF? I have used the low byte of TIMER1, however as the timers only count up and the roll-over the result is not really random enough.

 

The code I have used:

 

 

char i, char_h, char_l, char_rand;

unsigned short test_rnd;

 

// t1con = 0b00110000; //1:8 prescale

t1con = 0b00000000; // 1:1 prescale

set_bit( t1con, TMR1ON );

 

for (i = 0; i < 100; i++) {

MAKESHORT( test_rnd, tmr1l, tmr1h );

srand(test_rnd);

test_rnd = rand();

LOBYTE(char_l, test_rnd);

HIBYTE(char_h, test_rnd);

char_rand = char_l ^ char_h;

delay_ms(50);

}

 

Any help or comments will be appreciated.

 

PS: I posted old code and had to edit the post.

Edited by RSABear

Share this post


Link to post
Share on other sites
...The BoostC compiler supports two miscellaneous functions, unsigned short rand() and srand() from the library rand.lib. I would like to know if anybody has implemented these functions and have some guidelines for me to use them...

 

You can use rand/srand from BoostC (since both BoostC and BoostBasic use same engine their libraries are interchangeable). Something like this will do the job (don't forget to add rand.lib into the project):

 

Extern Function rand() As Word
Extern Sub srand( a As Word )

sub main()

Dim x As Word

' seed random number generator
Call srand( 100 )

' get 3 random numbers
x = Call rand()
x = Call rand()
x = Call rand()

' endless loop
Do while 1 
Loop
end sub

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...