CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Random byte from a Int32

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

Random byte from a Int32
PostPosted: Sat Mar 02, 2019 1:16 pm     Reply with quote

I have a random serial number from a chip ex: 0x12345678 and need a random byte from this, 0x00 to 0xFF.

The serial is from a chip so not two are the same!

Most simple was to use one of the byte from the 4 byte Int32 but the generated serial can change in any of the byte therefore we cant know which one to use...

Simple, how to get a single random byte from the Int32?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 02, 2019 1:51 pm     Reply with quote

Use the srand() and rand() functions:
Code:

#include <16F1847.H>
#fuses INTRC_IO, NOWDT
#use delay(clock = 4M)
#use rs232(baud = 9600, UART1, ERRORS)

#define RAND_MAX 255
#include <stdlib.h>

//====================================
void main()
{
int8 result;
int32 seed;

seed = 0x12345678;

srand(seed);

result = rand();

printf("%u  ", result);


while(TRUE);

}
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Sat Mar 02, 2019 2:29 pm     Reply with quote

Thanks
Only reason I skipped that was I thought the seed was a int8.
Super I just use that.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group