#### Archived

This topic is now archived and is closed to further replies.

# Stdlib.h

## Recommended Posts

I''m using Vc++ and i included the stdlib.h . But i''m trying to generate a random number but it does. I do this: short Test; Test = random(10); cout <<"Test"< How can i generate a random number in vc++ ? Thank''s "The shortcut is not always the best way " Metal Typhoon

##### Share on other sites
Martee    476
It''s rand(), not random().

~~~~~~~~~~
Martee

##### Share on other sites
i''ve tried that , look what it turn back to me

C:\Metal Softwares\Generator\Generator.cpp(23) : error C2660: ''rand'' : function does not take 1 parameters

and i did

rand(3);

Thanks

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
Martee    476
Sorry ... my mistake ...
rand() takes no parameters. It returns a number between 0 and RAND_MAX. So to generate a random number between 0 and 9, you would use 'rand() % 10'.

Edit: You'll also want to include time.h, and do a 'srand((unsigned int) time(0));'at the beginning of your program.

~~~~~~~~~~
Martee

Edited by - Martee on June 28, 2001 11:10:43 PM

##### Share on other sites
Guest Anonymous Poster
You should read carefully.

"C:\Metal Softwares\Generator\Generator.cpp(23) : error C2660: ''rand'' : function does not take 1 parameters"

And look into the help files more often.
This means no parameter...
So, it should be ...

Test = rand ( ); // rand ( empty )

Here is an example from the help files...

#include
#include
#include

void main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
// srand ( time ) to make it random based on time...
// or else u get the same number twice.

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}

"But why take the long way when the shortest is faster?"

BrownBean

##### Share on other sites
So if i just want to generate a random number between 0 2 i do this:

rand() % 3;

???

"The shortcut is not always the best way "

Metal Typhoon

Martee    476
Yes.

~~~~~~~~~~
Martee

##### Share on other sites
I have a vc++ version that i got on the internet so, i think that i can''t use the help.

Anyways i''ll buy the standard version, does that come with help stuff ?

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
nickr_21045    122
this doesn''t work

  #include #include int main(){ // generate random number between 0 and 9 std::cout << std::rand() % 10 << std::endl; return 0;}

##### Share on other sites
nickr_21045    122
This should work I did last one in c++ and
forgot the srand.

  #include #include #include int main(void){ srand(time(NULL)); printf("rand 0-9: %d\n", rand() % 10); return 0;}

##### Share on other sites
i got the rand() % 10; but i wanna know what does the srand(time(NULL)) does ?

And one more question if i buy vc++ standard does it come with any book ?

Thanks

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
nickr_21045    122
>> I have a vc++ version that i got on the internet
Warezing a compiler!!

Yea you can go read www.msdn.microsoft.com but it''s
kind of unstructured.

for ansi standard c functions
http://www.tcnj.edu/~cs/doc/CStdLib.html

##### Share on other sites
This is getting a little hard without dodumentation. I''ve mostly used borland , now i''m going to Vc++ so i need a good vc++ begginers book and a c++ book , that is based in standard library.

Does anybody know any of those books there ?

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
Martee    476
quote:
Original post by Metal Typhoon
what does the srand(time(NULL)) does ?

It generates a new random number seed based on the current CPU time. This will ensure that numbers generated by rand() are actually somewhat pseudo-random. Just for fuy, try omitting that line, and then running your program a few times .

~~~~~~~~~~
Martee

Edited by - Martee on June 28, 2001 12:31:50 AM

##### Share on other sites
gmcbay    130
srand(time(NULL));

seeds the random number generator with the current time.

Computers can''t generate real random numbers (without getting some random input, like white noise). The rand() function generates a list of numbers based on a fairly complex algorithm, but it is actually ordered and generally it will give you the same sequence of numbers every time you call it (because the next number is based on what the last number is).

So, try writing a program that simple calls rand() 10 times and prints the results, run it a bunch of times. You''ll probably notice it spits out the same 10 numbers every time. Not very random right? srand allows you to ''seed'' the number generator. Using the current time is a fairly fast & somewhat randomish way of getting a unique starting point for the numbers, so that they appear to be more truly random.

##### Share on other sites
Yeah now i got it. Thanks

And a have a question . Is that part of the standard library ? If it''s can u tell me any book just about STL !!? Thanks again guys !!

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
gmcbay    130
rand() and srand() are parts of the standard C runtime library. Not the STL (which is C++ specific). A good book on C that covers the standard C runtime library is The C Programming Language by Kernighan & Ritchie. Considering they invented C, they know a thing or two about the language. Any bookstore with a good computer section (which is fairly rare, unfortunately) should have this. Or order online through B&N, or FatBrain.

A good book (well, books, really) on C++, including the STL is Bruce Eckel's Thinking in C++. You can even download this (in HTML or PDF format) which is very nice. Look here for more information:

http://www.mindview.net/Books

A lot of people also like the C++/STL books written by Scott Meyers. I don't personally have any experience with them, but considering how popular they are I'm guessing they are pretty good. Again, these books should be in most good bookstores or online bookstores.

Edited by - gmcbay on June 29, 2001 1:06:05 AM

##### Share on other sites
Thank''s man ! i really appreciate your help. I want to buy the STL, cuz i don''t like C as much as c++.

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
Do you know if this is a god bok in STL ? The C++ Standard Library : A Tutorial and Reference ?

Here, see it !

"The shortcut is not always the best way "

Metal Typhoon

##### Share on other sites
Sandman    2210
STL does a completely different job to the C run time libraries. STL is great for off-the-shelf linked lists, vectors, iterators and other template classes (hence the name standard template library) Im guessing that you havent learnt what templates are yet, (let alone linked lists, vectors and iterators) so forget about them for now, they are confusing and complicated and not something you want to jump into until you have the basics down (like being able to debug your own code for example)

Read your sig - dont try and learn to code the short way - you have to do it the long way. Get a basic beginners C/C++ book. Do the examples.

##### Share on other sites
Cyberdrek    100
( I removed my post as I was way off topic by the time I wrote it. )

"And that's the bottom line cause I said so!"

Cyberdrek