Cppnoobie
Cppnoobie

Reputation: 61

C++ random numbers

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>

Upvotes: 5

Views: 19243

Answers (4)

Sogger
Sogger

Reputation: 16142

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)

Upvotes: 10

Babacar Diass&#233;
Babacar Diass&#233;

Reputation: 89

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/

Upvotes: 2

Alan
Alan

Reputation: 281

You need to include <time.h>

Upvotes: 4

SolarBear
SolarBear

Reputation: 4629

You'll need

#include <ctime>

to access the time function.

Upvotes: 13

Related Questions