RSABear 0 Posted June 12, 2007 Report Share Posted June 12, 2007 (edited) 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 June 13, 2007 by RSABear Quote Link to post Share on other sites
Pavel 0 Posted June 14, 2007 Report Share Posted June 14, 2007 ...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 Quote Link to post Share on other sites
Recommended Posts
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.