# Thread: The syntax to declaire a variable in a function.

1. ## The syntax to assign a variable to a function.

Hello, i'm trying to use the rand() function and i would like to know how to make the random number a variable.
I've tryed:
Code:
`rand(int c);`
My intention is to compare two variable's in a statement, one random. I hope you all understand.
Thank's.

2. Code:
`rand("%d", number);`
number will hold a number, provided you "assigned" something to number somewhere else in your program

Simple example:
Code:
```int main()
{
int number = 3;
printf("%d", number);
return 0;
}```
or:
Code:
```int main()
{
go(5);
return 0;
}

int go(int number)
{
printf("%d", number);
return 0;
}```

3. hmm
are you trying to generate a random number also?
remember to use srand(time(NULL));

4. ## Thank's for the reply.

Ok i understand how to assign the variable to the rand() function.
Code:
```int curb()
{
int z, c=5;
rand("%d", c);
sleep(1);
clrscr();
printf("Were coming up to a waxed curb, what do you want to do, make it fast\n");
printf("1. do an olie over it.\n");
printf("2. do a flip kick onto it.\n");
printf("3. do a big jump over it.\n");
printf("4. do a switch onto it.\n");
printf("5. exit\n");
scanf("%d", &z);
if (z == c)
b += OLIE;
else if (z == c)
b += FLIP_KICK;
else if (z == c)
b += BIG_JUMP;
else if (z == c)
b += SWITCH;
else if (z == c)
exit();
else
printf("You fell\n");
printf("%d", b);
printf(" point's\n");
return z;
}```
I do want to make the vaiable c a random number between 1 and 6. I don't want other's to write my code for me but where do i go from here with srand(time(NULL))? Just hint's, i want to learn not to have other's do my thinking for me.

5. I do want to make the vaiable c a random number between 1 and 6. I don't want other's to write my code for me but where do i go from here with srand(time(NULL))? Just hint's, i want to learn not to have other's do my thinking for me.
Oh, I donno... How about you read the FAQ for this web site? HMMMMM?

Quzah.

6. You only need call srand once in your program, so place it after your variable declarations in main.
Your if else ladder wont work properly as your using the same condition for every test. A switch statement would be much better here.
to get a number from 1 to 6 you need to use the '%' operator
Code:
`c = rand() % 6;`
This will give you a number from 0 to 5, so you need to add 1 to c or change your code for the range 0 to 5.

7. ## Explaination and thank's.

In reply to quzah...
Oh, I donno... How about you read the FAQ for this web site? HMMMMM?
I read the faq many of time's, but after reading the part on random number's i still didn't understand how to incorperate it into my program.
Thank's for the hint's C_coder.(everything's a hint at my level of programming knowledge...)

Your if else ladder wont work properly as your using the same condition for every test
I don't know what i'm going to do with that yet, when i get my random number's going i can trash the whole program and start over .

8. ## Problem solved

this work's for me:
Code:
```#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
srand(time(NULL));
int c = rand() % 7;
printf("%d", c);
return 0;
}```

9. int c = rand() % 7;
bear in mind your getting 0 to 6 here, not 1 to 6

10. ## What i was after...

Was a 1 and a 0.

Popular pages Recent additions