soniccool
soniccool

Reputation: 6058

Random not declared in scope

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);
    }
}

Upvotes: 8

Views: 46239

Answers (5)

AJcleverprogrammer
AJcleverprogrammer

Reputation: 61

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.

Upvotes: 0

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

Upvotes: -3

Rishabh Agrahari
Rishabh Agrahari

Reputation: 3717

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

#include <cstdlib>

or

#include <stdlib.h>

Upvotes: 8

Fred Foo
Fred Foo

Reputation: 363617

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.

Upvotes: 9

Ari
Ari

Reputation: 1092

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/

Upvotes: 1

Related Questions