5

How can I use random numbers in Linux and C++?

I have found some code I would like to use, it has the line

srand((unsigned)time(0));//seed 

but gcc says

board.cpp:94:24: error: ‘time’ was not declared in this scope

I have included the following files

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> #include <algorithm> 
6
  • 6
    include time.h Commented Jun 15, 2011 at 15:42
  • 2
    you may have a look at boost.org/doc/libs/1_46_1/doc/html/boost_random.html Commented Jun 15, 2011 at 15:46
  • @DumbCoder: Why won't you post that as an answer? I noticed you seldom post the answers before anyone else but as comments. Commented Jun 15, 2011 at 15:46
  • @Als - Will post answers from now on, no comments :). Commented Jun 15, 2011 at 15:53
  • @DumbCoder: I said so because answers earn you rep, Comments don't. :) Commented Jun 15, 2011 at 15:54

4 Answers 4

13

You'll need

#include <ctime> 

to access the time function.

Sign up to request clarification or add additional context in comments.

3 Comments

For any modern C++ compiler, I think #include <ctime> is preferred.
The OP should probably prefer #include <cstdio>, #include <cstdlib>, and #include <cstring> as well.
I prefer to be consistent. If the file I am working on uses <stdlib.h> then I would add <time.h>. If it is a new file then <ctime> would be my preference (but I still need to force myself to think about, even after all these years it is still not automatic).
10

You haven't included the header that defines the function, in order for your program to know how to call time() you need to add this include:

#include <time.h> 

and just to nitpick (code style) your call should more correctly be time(NULL) instead of time(0)

4 Comments

Just to nitpick, time(NULL) is not more correct than time(0). It's a style preference, since both NULL and 0 are defined by the standard to have integral type, and to convert to a null pointer. Bjarne Stroustrup has convinced me not to bother with NULL in C++ (www2.research.att.com/~bs/bs_faq2.html#null). As an aside, in C NULL is downright dangerous, since in C you don't know whether it's a pointer or integer, so if you use it in a varargs call you're in trouble.
@Steve Jessop: I still like NULL as it stands out (especially with syntax highlighting in editors). But I will be much happier with the C++0x nullptr keyword
I too will appreciate the 'nullptr'. For now, I prefer NULL since I think it makes it a little more apparent a function like time(NULL) is expecting a pointer and not some int argument. Since time() is so well know this is not really an issue here. On the other hand NULL drives me nuts when people use it as a value for non-pointer arguments like an int value of flags. Then there is the whole issue of non-zero values for NULL pointers on non-standard architectures...
Actually, I'm pretty sure the best practice is to avoid NULL wherever possible. As the guys above me said, it's dangerous. It causes many problems, being an anti-pattern in language design.
4

You need to include <time.h>

Comments

2

Indeed, It misses the include for the time function try :

#include <time.h> 

you can go there to have a little more explanation about srand:

http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.