srand und rand()
Verfasst: Mi Dez 23, 2015 7:08 pm
Hallo könnte mir jemand erleutern was genau passiert in der Zeile
s = printf(" %d", (rand() % 6+1); /* von 1 bis 6 hier es ist mir klar*/wenn ich noch mit einer Zahl multipliziere Bsp.
s = printf(" %d", (rand() % 6+1) *2); wie hier mal 2
Code: Alles auswählen
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i, s;
time_t t;
time(&t);
srand((unsigned int)t); /* Zufallsgenerator initialisieren */
for ( i=0; i<20; i++ )
/* printf("%d ", rand() % 6 +1); /* Wuerfeln von 1 bis 6*/
s = printf(" %d", (rand() % 3+2) *2);
printf("%d", s);
printf("\n");
return 0;
}