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:
My intention is to compare two variable's in a statement, one random. I hope you all understand.Code:rand(int c);
Thank's.
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:
My intention is to compare two variable's in a statement, one random. I hope you all understand.Code:rand(int c);
Thank's.
Last edited by loopy; 05-25-2002 at 05:47 AM.
WorkStation(new, a month ago):
Sony Vaio i686 Desktop
2.60 GIGhz Intel Pentium 4(HT)
512Mb DDR RAM
800MHz Front Side Bus!
120 GIG IDE HardDrive
Matrox G400 Dual-Head
Linux kernel 2.6.3
Modified Slackware 9.1
GCC/GDB
Multi-mon
Simultaneous Multiple Processes
number will hold a number, provided you "assigned" something to number somewhere else in your programCode:rand("%d", number);
Simple example:
or:Code:int main() { int number = 3; printf("%d", number); return 0; }
Code:int main() { go(5); return 0; } int go(int number) { printf("%d", number); return 0; }
The world is waiting. I must leave you now.
hmm
are you trying to generate a random number also?
remember to use srand(time(NULL));
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
Ok i understand how to assign the variable to the rand() function.
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.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; }
WorkStation(new, a month ago):
Sony Vaio i686 Desktop
2.60 GIGhz Intel Pentium 4(HT)
512Mb DDR RAM
800MHz Front Side Bus!
120 GIG IDE HardDrive
Matrox G400 Dual-Head
Linux kernel 2.6.3
Modified Slackware 9.1
GCC/GDB
Multi-mon
Simultaneous Multiple Processes
Oh, I donno... How about you read the FAQ for this web site? HMMMMM?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.
Quzah.
Hope is the first step on the road to disappointment.
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
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.Code:c = rand() % 6;
All spelling mistakes, syntatical errors and stupid comments are intentional.
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.In reply to quzah...
Oh, I donno... How about you read the FAQ for this web site? HMMMMM?
Thank's for the hint's C_coder.(everything's a hint at my level of programming knowledge...)
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 .Your if else ladder wont work properly as your using the same condition for every test
Last edited by loopy; 05-25-2002 at 03:09 AM.
WorkStation(new, a month ago):
Sony Vaio i686 Desktop
2.60 GIGhz Intel Pentium 4(HT)
512Mb DDR RAM
800MHz Front Side Bus!
120 GIG IDE HardDrive
Matrox G400 Dual-Head
Linux kernel 2.6.3
Modified Slackware 9.1
GCC/GDB
Multi-mon
Simultaneous Multiple Processes
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; }
Last edited by loopy; 05-25-2002 at 05:35 AM.
WorkStation(new, a month ago):
Sony Vaio i686 Desktop
2.60 GIGhz Intel Pentium 4(HT)
512Mb DDR RAM
800MHz Front Side Bus!
120 GIG IDE HardDrive
Matrox G400 Dual-Head
Linux kernel 2.6.3
Modified Slackware 9.1
GCC/GDB
Multi-mon
Simultaneous Multiple Processes
bear in mind your getting 0 to 6 here, not 1 to 6int c = rand() % 7;
All spelling mistakes, syntatical errors and stupid comments are intentional.
Was a 1 and a 0.
WorkStation(new, a month ago):
Sony Vaio i686 Desktop
2.60 GIGhz Intel Pentium 4(HT)
512Mb DDR RAM
800MHz Front Side Bus!
120 GIG IDE HardDrive
Matrox G400 Dual-Head
Linux kernel 2.6.3
Modified Slackware 9.1
GCC/GDB
Multi-mon
Simultaneous Multiple Processes