简体   繁体   中英

Does rand() function exist in mikroc?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM