Problem with rand_max
I want to generate random numbers between 1-10. I thought this was accurate code. But, in every case I get a really large number.
In one case I got tall1 = 6749516
and tall2 = 6749520
What am I writing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
//Include header files
#include<stdio.h> // printf, scanf
#include<stdlib.h> //rand + srand
#include<time.h> //time
int main(void)
{
//Initializing constants
int tall1 = 0;
int tall2 = 0;
srand(time(NULL));
//Draw to random numbers from 1-10
tall1 = rand() % 10 + 1;
printf("Tall1 = %d \n", &tall1);
tall2 = rand() % 10 + 1;
printf("Tall2 = %d \n", &tall2);
|
I know that srand(time(NULL)) counts every second from 1970 (or something) - is that what I'm getting?
The problem is with the "&tall1" You see that little "&" symbol before it? Remove it and it will fix your problem.
OH. Thanks! That worked. (Phew).
Do you know what happened when I printed &tall1?
&tall1 gives you a pointer to tall1, but a pointer type does not match the format specifier %d so the behavior is undefined.
Topic archived. No new replies allowed.