8

I am getting the error:

tester.cpp|20|error: 'rand' was not declared in this scope|

Did I miss to include something here?

void tester::volumeset(bool A_B, int i, int v)
{
    if (A_B == true)
    {
        A_volumn[i] = rand(v+1);
    }else{
        B_volumn[i] = rand(v+1);
    }
}
soniccool
  • 5,790
  • 22
  • 60
  • 98
  • Have you added the correct `#include` file or even declared the function? – Tony The Lion Nov 26 '12 at 15:58
  • Yea i have this #include #include #include #include #include – soniccool Nov 26 '12 at 15:58
  • I saw this on c++ forums unless its not an actualfunction in c++? – soniccool Nov 26 '12 at 15:59
  • 1
    I've never seen that C++ function. C has a `rand()` function – Tony The Lion Nov 26 '12 at 15:59
  • 1
    Just so you know, `#include`ing `` and `` are the same thing. For system headers (wrapped in `<>`) that came from C, you can either use their original name with a `.h`, as in `` or use the newer C++ name which drops the `.h` and adds a `c` at the beginning: ``. – Ari Nov 26 '12 at 16:04
  • Indirectly related, not a dupe: http://stackoverflow.com/questions/686353/c-random-float/686373 – John Dibling Nov 26 '12 at 16:05
  • @Ari: they're not the same. `` puts standard C functions in the `std` namespace and may add a bunch of overloads (http://stackoverflow.com/a/8734292/166749). – Fred Foo Nov 26 '12 at 16:07

5 Answers5

9

random is not a standard C++ function; it's a POSIX function, so it's not available on Windows. Use rand instead, or better, the new C++11 randomness library.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
8

rand is part of cstdlib, try including cstdlib in your code.

#include <cstdlib>

or

#include <stdlib.h>

Rishabh Agrahari
  • 3,447
  • 2
  • 21
  • 22
1

You want to use rand(), not random(). You can see some examples of how to use it here: http://www.cplusplus.com/reference/cstdlib/rand/

Ari
  • 1,102
  • 9
  • 17
0

when i compile my code with Code::Blocks i don't need to #include <cstdlib>, with DevC++ i simply added #include <cstdlib> and it works for me.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • What's the difference from [Rishabh Agrahari's answer](https://stackoverflow.com/a/45393947/5376789)? – xskxzr Jul 11 '19 at 14:19
-3

enter code hereonly including #include or #include may not work it is better to use for instance I get a challenge when I use rand(100) but instead of this when I use rand()%100 it works well. try and enjoy it.

for(i=0;i<cust;i++)
{

  rsr[i]=rand(100);
 }

it works when I change it to 
 for(i=0;i<cust;i++)
 {

  rsr[i]=rand()%100;
  }

if you want to make it 1-100 use below but 0-99 use the above

for(i=0;i<cust;i++)
 {

  rsr[i]=rand()%(100+1);
  }

for(i=0;i<cust;i++)
 {

  rsr[i]=rand()%100+1;
  }