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,276
  • 21
  • 58
  • 96

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
  • 342,876
  • 71
  • 713
  • 819
7

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

#include <cstdlib>

or

#include <stdlib.h>

Rishabh Agrahari
  • 3,161
  • 2
  • 20
  • 21
2

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
  • 161,055
  • 37
  • 262
  • 269
  • 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;
  }