rand ( )
-
I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means.
#include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; }
Thanks, Dave :-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means.
#include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; }
Thanks, Dave :-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
If you don't use srand(TIME(NULL)) to seed rand(), you'll get the same random numbers every time! if the maximum should be 150 you can do the following:
int nRandomNumber = (rand() % 150) + 1;
your code will produce linker errors! int rand() is a declaration. the rand function does not take parameters and your main function does not return a value! hope this helps...
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means.
#include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; }
Thanks, Dave :-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
srand() seeds the pseudo random number generator. Many, if not most, developers just use the time function for this. rand() returns a pseudo random number from 0 to RAND_MAX. To get a random number in an range, use the modulo operator with the max value plus one. In other words to obtain a random number from 0 through 5 do the following:
int x = rand() % 6;
Never heard of nrand(). -
srand() seeds the pseudo random number generator. Many, if not most, developers just use the time function for this. rand() returns a pseudo random number from 0 to RAND_MAX. To get a random number in an range, use the modulo operator with the max value plus one. In other words to obtain a random number from 0 through 5 do the following:
int x = rand() % 6;
Never heard of nrand(). -
If you don't use srand(TIME(NULL)) to seed rand(), you'll get the same random numbers every time! if the maximum should be 150 you can do the following:
int nRandomNumber = (rand() % 150) + 1;
your code will produce linker errors! int rand() is a declaration. the rand function does not take parameters and your main function does not return a value! hope this helps...
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
Bob, I'm a bit confused on the rand function being a declaration. Does this mean that rand ( ) should only appear in a header file and not in the source file. How can you tell when a function is a declaration or if it belongs in the source file? Is it because of the "int" in front of rand( ) that makes it a declaration? Thanks, Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
Bob, I'm a bit confused on the rand function being a declaration. Does this mean that rand ( ) should only appear in a header file and not in the source file. How can you tell when a function is a declaration or if it belongs in the source file? Is it because of the "int" in front of rand( ) that makes it a declaration? Thanks, Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
DaveE9th wrote: Does this mean that rand ( ) should only appear in a header file and not in the source file. No it does not and most likely it will be in the source file. John
-
I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means.
#include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; }
Thanks, Dave :-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
The following two code examples do compile, but only yield a message that says to press a key to continue. Shouldn't I see a random number "x" cout to the screen from rand ( ). What am I doing wrong here?
#include <cstdlib #include <iostream> using namespace std; void main() { int x = (rand() % 150) + 1; cout< Here's the other example... `#include <cstdlib> #include <iostream> using namespace std; void main() { int x = rand() % 151; cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_`
-
The following two code examples do compile, but only yield a message that says to press a key to continue. Shouldn't I see a random number "x" cout to the screen from rand ( ). What am I doing wrong here?
#include <cstdlib #include <iostream> using namespace std; void main() { int x = (rand() % 150) + 1; cout< Here's the other example... `#include <cstdlib> #include <iostream> using namespace std; void main() { int x = rand() % 151; cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_`
-
I can't believe what my problem was. I'm still learning VC++ 6.0. I wanted to try some new code so I just replaced the code on my screen with the new code. I went to compile and evertyhing looked ok, it wasn't. I found something weird (at least I think it is weird) about VC++ 6.0, it doesn't compile what's on your screen unless you save it first. The source cpp file listed in my view folder did NOT change with the new code I typed in. VC++ compiled the cpp file listed in the view folder, not the cpp file I had open in front of me. So the code in front of me (that was all messed up) was seemingly compiling ok, in reality it was the cpp file in my folder that was compiling ok instead. I did get rand( ) to work finally. I used time(0) as the argument and it works great. Thanks much, Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
Bob, I'm a bit confused on the rand function being a declaration. Does this mean that rand ( ) should only appear in a header file and not in the source file. How can you tell when a function is a declaration or if it belongs in the source file? Is it because of the "int" in front of rand( ) that makes it a declaration? Thanks, Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
A declaration looks like this: return type name(arg list);. It's predefined, that means that you only have to use it! Therefore it should only appear in a source file! You should seperate 3 things: 1) declaration => Header File 2) Implementation => Source File 3) Call to a function => Source File (within other functions) A Implementation looks like this: return type name(arg list) { // source code } I assume you know how to call a function... hope this helps
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.