How can I implement the rand() C++ function in mikroC?
I tried rand() but does not work... I don't know how to resolve this
How can I implement the rand() C++ function in mikroC?
I tried rand() but does not work... I don't know how to resolve this
If your C implementation conforms to C89, it should include a working rand()
— maybe you forgot to include <stdlib.h>
?
If not, it is trivial to write your own rand
, as long as you don't require very high quality of generated numbers, which you shouldn't for the purposes of TETRIS. This tiny implementation is promoted by POSIX as an option for programs that need to repeat the same sequence of pseudo-random numbers across architectures:
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
It will not give you great pseudo-randomness, but it won't be worse than many real-life implementation of rand()
, either.