I need to create a random integer between 0 and 255, what function would I use and how would I accomplish this?
I need to create a random integer between 0 and 255, what function would I use and how would I accomplish this?
rand()%256
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Also, how can I cast this number to a char?
EDIT:Not a cast, but a function.Code:char thechar[3]; int num = rand()%256; itoa(num, thechar, 10); //Note that this is not ANSI standard C.
Ah ok thanks.
My compiler didn't like itoa at all, are there any alternatives I can use?
I particularly don't see the problem with casting your number to a char, so long as it's between 0 and 255.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
Yes but how do I do it?
Voila.Code:int d = 65; char c = (char)d;
In fact, I remember somewhere reading that you should keep chars in ints anyways...but I don't know if that's accurate at all.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
Ah cheers.
Code:char thechar[3]; int num = rand()%256; sprintf(thechar, "%d", num);
I'm having a problem with the the random function. It's not very random.
Everytime I run the program, it is picking the same numbers.
Seed rand:Code:srand(time(NULL));
You should call it once - for example in the beginning of main
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I get this message:
warning: implicit declaration of function ‘time’