Jump to content

  • Log In with Google      Sign In   
  • Create Account

Stdlib.h


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 03:47 PM

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

Sponsor:

#2 Martee   Members   -  Reputation: 476

Like
Likes
Like

Posted 28 June 2001 - 03:51 PM

It''s rand(), not random().

~~~~~~~~~~
Martee

#3 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 04:01 PM

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

#4 Martee   Members   -  Reputation: 476

Like
Likes
Like

Posted 28 June 2001 - 04:09 PM

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

#5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 28 June 2001 - 04:18 PM

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

#6 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 04:20 PM

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

#7 Martee   Members   -  Reputation: 476

Like
Likes
Like

Posted 28 June 2001 - 04:21 PM

Yes.

~~~~~~~~~~
Martee

#8 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 04:33 PM

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

#9 nickr_21045   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 04:57 PM

this doesn''t work

  
#include <iostream>
#include <cstdlib>

int main()
{
// generate random number between 0 and 9

std::cout << std::rand() % 10 << std::endl;
return 0;
}



#10 nickr_21045   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 05:02 PM

This should work I did last one in c++ and
forgot the srand.

  
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

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



#11 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 05:08 PM

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

#12 nickr_21045   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 05:15 PM

>> 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


#13 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 05:22 PM

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

#14 Martee   Members   -  Reputation: 476

Like
Likes
Like

Posted 28 June 2001 - 05:30 PM

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

#15 gmcbay   Members   -  Reputation: 130

Like
Likes
Like

Posted 28 June 2001 - 05:34 PM

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.


#16 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 June 2001 - 05:48 PM

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

#17 gmcbay   Members   -  Reputation: 130

Like
Likes
Like

Posted 28 June 2001 - 06:04 PM

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

#18 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 June 2001 - 05:31 AM

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

#19 Metal Typhoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 June 2001 - 06:01 AM

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

#20 Sandman   Moderators   -  Reputation: 2136

Like
Likes
Like

Posted 29 June 2001 - 06:11 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS