Home > DeveloperSection > Forums > How to use rand( ) in c ?
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    June-14-2013 11:59 PM

 Visual C++ Visual C++ 
Ratings:


 1 Reply(s)
 872  View(s)
Rate this:
Hi Expert,

How to use rand( ) in c++?

Thanks in advance. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-15-2013 4:40 AM

Hi,

for using rand() you can see following example

class base{
public:
int i;
base(int x){int x;}
virtual void func()
{
cout<<"Using base version of func():";
cout<<i<<'\n';
}
};
class derived1: public base{
public:
derived1(int x);base(x) { }
void func()
{
cout<<" Using derived1's version of func():";
cout<<i*i<<'\n';
}
};
class derived2:public base{
derived2(int x):base(x){ }
void func()
{
 cout<<"Using derived2's version of func():";
cout<<i+i<<'\n';
}
};
int main()
{
base *p;
derived1 d_ob1(10);
derived2 d_ob2(10);
int i,j;
for(i=0;i<10;i++)
{
j=rand( );
if((j%2)) p = &d_ob2;
p->func( ); 
}
return 0;
}

Don't want to miss updates? Please click the below button!

Follow MindStick