How to Generate Random integer/byte?

Dear All,

May i know how i could generate random number on rabbit?
currently i’m using rcm3305 with dc 9.25

i was try using srand and rand function.
but the return value from rand function is float.
is it a way to get random integer/byte?

this is my code:
i = rtc.tm_sec;
srand (i);
/* KEY1 and KEY2 is float, i can’t change to
byte or integer even with casting…
any help?
KEY1 = rand(0,255);
KEY2 = rand();


So rand generates a number between 0.0 and 1.0, and you want a number between 0 and 256 (byte).

Then you do the following
KEY1 = (rand()*1000)%256;

I havent tested it but it should work :slight_smile:

Edit - You do change the random distribution a bit this way, you should decide if that is important for your application or not.

The normal method to generate a random integer value between 0 and N-1 would be:

KEY1 = floor(rand() * N);