1
votes

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

1
Read about simple realisations. Also you can use a noise of ADC to improve randomness.Eddy_Em
Has nothing to do with the compiler but rather with the libc you're using.m0skit0
I want to implement a tetris game but i don't know how to generate the shapes randomlyZolyboy
Are you sure? When mikroc really is ANSI-compilant like it claims, it should include rand() in stdlib.h.Philipp

1 Answers

1
votes

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.