1

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

Zolyboy
  • 457
  • 7
  • 17
  • 1
    Read about simple realisations. Also you can use a noise of ADC to improve randomness. – Eddy_Em Mar 05 '13 at 09:07
  • 2
    Has nothing to do with the compiler but rather with the libc you're using. – m0skit0 Mar 05 '13 at 09:08
  • I want to implement a tetris game but i don't know how to generate the shapes randomly – Zolyboy Mar 05 '13 at 09:09
  • [**Hoe to generate/wirte random() numbers**](http://stackoverflow.com/questions/15038174/generate-random-numbers-without-using-any-external-functions/15040471#15040471) – Grijesh Chauhan Mar 05 '13 at 09:09
  • Are you sure? When mikroc really is ANSI-compilant like it claims, it should include rand() in stdlib.h. – Philipp Mar 05 '13 at 09:11
  • Yes, you are right I can call the rand function.... but it's not working well as I observed, it generates me all the time the same number – Zolyboy Mar 05 '13 at 09:15
  • @Zolyboy: Are you calling `srand()`? Are you calling it more than once? – Hasturkun Mar 05 '13 at 09:19
  • I called rand() only once until now... but I think that i will have to put it inside an infinite loop – Zolyboy Mar 05 '13 at 09:21
  • @Philipp MikroC seems to be a freestanding implementation. Freestanding implementations don't require stdlib.h. See section 4, paragraph 6 of n1570.pdf. – autistic Mar 05 '13 at 09:51
  • @Zolyboy: `rand()` is a pseudo-random-number generator. If you don't call `srand` then it is *guaranteed* to always generate the same sequence of numbers in each run of your program. If you do call `srand` then `rand()` will generate a different sequence of numbers according to the value you provide. – Steve Jessop Mar 05 '13 at 11:00
  • @Steve Jessop I understand but what value should I pass to the srand() seed paramater, cause time(NULL) is not defined – Zolyboy Mar 05 '13 at 12:24
  • @Zolyboy: you just need to pass a different value each time you run the program. What makes you say that `time(NULL)` is not defined? – Steve Jessop Mar 05 '13 at 12:28
  • I tried it like srand(time(NULL)), but this generates error – Zolyboy Mar 05 '13 at 14:06
  • maybe I forgot to include something or I don't know... I did not work with mikroC until yesterday – Zolyboy Mar 05 '13 at 14:07
  • In C++ I know that i have to include the time.h header but in mikroC i don't know how to do this – Zolyboy Mar 05 '13 at 14:17

1 Answers1

1

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.

user4815162342
  • 141,790
  • 18
  • 296
  • 355