Not Yet Answered # Rand error

Narue 5,707 Discussion Starter darkunknown nezachem 616 Discussion Starter darkunknown Discussion Starter darkunknown Discussion Starter darkunknown Excizted 67 Discussion Starter darkunknown Discussion Starter darkunknown Discussion Starter darkunknown Discussion Starter darkunknown nezachem 616 Discussion Starter darkunknown

0

The time function comes from the <ctime> header (failure to include it is probably your error), and keep in mind that everything in the c* headers should be in the std namespace:

```
#include <cstdlib>
#include <ctime>
int main()
{
std::srand ( (unsigned)std::time ( 0 ) );
}
```

Though some compilers are more lax with that particular namespace requirement, so if you get an error that srand or time are not a part of namespace std, that's why.

*Edited 6 Years Ago by Narue*: n/a

0

ok, ill try. im still really new at this.

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int N, iTotalMoves, iTotalHome, x, iTotalPub;

float fAverageMoves, fAverageHome, fProbHome;

int B = 1000000;

void Walk(float &fAverageMoves, int &iTotalMoves,

int &iTotalHome, float &fAverageHome, int &N, int &x,

int &iTotalPub, float fProbHome) ;

int Spot;

srand(time(0));

int main()

{

Walk(fAverageMoves, iTotalMoves, iTotalHome, fAverageHome, N, x,

iTotalPub, fProbHome);

return 0;

}

void Walk(float &fAverageMoves, int &iTotalMoves, int &iTotalHome,

float &fAverageHome, int &N, int &x, int &iTotalPub, float fProbHome)

{

fProbHome = rand() % 3;

N=2;

while(2<= N&& 7>=N && N++)

{

if(iTotalHome+iTotalPub <= 1000000)

{N=x;

do

{

if (1< x<8)

{

if(fProbHome == 1 || fProbHome == 2)

{

x=x+1;

}

if(fProbHome == 0)

{

x = x-1;

}

}

}while(x!= 1 && x!= 8);

if(x==1)

{

iTotalHome++;

}

if(x==8)

{

iTotalPub++;

}

iTotalMoves ++;

}

fAverageMoves = iTotalMoves/(N-1);

fAverageHome = iTotalHome/iTotalMoves;

cout << "Average Moves:" << fAverageMoves<< endl;

cout << "Total Home:" << iTotalHome<< endl;

cout << "Total Pub:" << iTotalPub<< endl;

cout << "Total Moves:" << iTotalMoves<< endl;

}

return;

}

bash-3.2$ fg++ program4.cpp -o program4

program4.cpp:27: error: expected constructor, destructor, or type conversion before ‘(’ token

does that help at all?

0

`srand(time(0)); int main() { Walk(fAverageMoves, iTotalMoves, iTotalHome, fAverageHome, N, x, iTotalPub, fProbHome); return 0; }`

You are trying to call functions outside of any function. Put `srand(time(0));`

inside main.

*Edited 6 Years Ago by nezachem*: n/a

0

i fixed it! i had to put the list of variables inside the int main().

then it worked

0

ok im stuck now.

**this is what im supposed to do:**

The classic drunkard's walk problem: Over an eight block line,

the home of an intoxicated chap is at block 8, and a pub is at block 1.

Our poor friend starts at block n, 2 <= n <= 7, and wanders at random,

one block at a time, either toward or away from home. At any intersection,

he moves toward the pub with a certain probability, say 2/3, and towards

home with a certain probability, say 1/3. Having gotten either home or to

the pub, he remains there. Write a program to simulate 1000000 trips in which

he starts at block 2, another 1000000 in which he starts at block 3, and so

forth up through block 7. For each starting point, calculate and print the

proportion of the time he ends up at home and the average number of blocks

he walked on each trip.

For the random number generation...

Use the 'srand' function to seed the random number generator.

Use the 'rand' function (and mod as needed) to get random probabilities

for a given step.

To do each single walk, call a function that runs through a loop

until the walk is done.

**and im supposed to get approximately this:**

I = 2

TotalMoves = 2834110

Avg moves = 2.834110

total Home = 7820

avg home = 0.0078200

I = 3

TotalMoves = 5503639

Avg moves = 5.503639

total Home = 23621

avg home = 0.0236210

I = 4

TotalMoves = 7830959

Avg moves = 7.830959

total Home = 54851

avg home = 0.0548510

I = 5

TotalMoves = 9519658

Avg moves = 9.519658

total Home = 118166

avg home = 0.1181660

I = 6

TotalMoves = 9875095

Avg moves = 9.875095

total Home = 243413

avg home = 0.2434130

I = 7

TotalMoves = 7581532

Avg moves = 7.581532

total Home = 496420

avg home = 0.4964200

ill post what i have now in another reply

0

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

int N, iTotalMoves, iTotalHome, x, iTotalPub, iProbHome;

float fAverageMoves, fAverageHome;

void Walk(float &fAverageMoves, int &iTotalMoves,

int &iTotalHome, float &fAverageHome, int &N, int &x,

int &iTotalPub, int iProbHome) ;

srand(time(0));

Walk(fAverageMoves, iTotalMoves, iTotalHome, fAverageHome, N, x,

iTotalPub, iProbHome);

return 0;

}

void Walk(float &fAverageMoves, int &iTotalMoves, int &iTotalHome,

float &fAverageHome, int &N, int &x, int &iTotalPub, int iProbHome)

{

N=2;

iTotalHome=0;

iTotalPub=0;

do

{

if(iTotalHome+iTotalPub <= 1000000&& iTotalHome+iTotalPub >=0)

{

N=x;

do

{

iProbHome = rand()%3;

if(iProbHome == 1 || iProbHome == 2)

{

x=x+1;

iTotalMoves++;

}

if(iProbHome == 0 && x != 1 && x !=8)

{

x = x-1;

iTotalMoves++;

}

}while(x!= 1 && x!= 8);

if(x==1)

{

iTotalHome++;

}

if(x==8)

{

iTotalPub++;

}

}

fAverageMoves = iTotalMoves/1000000;

fAverageHome = iTotalHome/1000000;

cout << "I = " << N << endl;

cout << "Average Moves:" << fAverageMoves<< endl;

cout << "Total Home:" << iTotalHome<< endl;

cout << "Average Home:" << fAverageHome << endl;

cout << "Total Moves:" << iTotalMoves<< endl<< endl;

N++;

}while(2<=N && 7>=N);

return;

**can anybody help me?**

0

omg, use code tags.

[-code][/-code]

without the dashes, that'd make people like me want to help you ;D

*Edited 6 Years Ago by Excizted*: n/a

0

like this?

```
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int N, iTotalMoves, iTotalHome, x, iTotalPub, iProbHome;
float fAverageMoves, fAverageHome;
void Walk(float &fAverageMoves, int &iTotalMoves,
int &iTotalHome, float &fAverageHome, int &N, int &x,
int &iTotalPub, int iProbHome) ;
srand(time(0));
Walk(fAverageMoves, iTotalMoves, iTotalHome, fAverageHome, N, x,
iTotalPub, iProbHome);
return 0;
}
void Walk(float &fAverageMoves, int &iTotalMoves, int &iTotalHome,
float &fAverageHome, int &N, int &x, int &iTotalPub, int iProbHome)
{
N=2;
iTotalHome=0;
iTotalPub=0;
do
{
if(iTotalHome+iTotalPub <= 1000000&& iTotalHome+iTotalPub >=0)
{
N=x;
do
{
iProbHome = rand()%3;
if(iProbHome == 1 || iProbHome == 2)
{
x=x+1;
iTotalMoves++;
}
if(iProbHome == 0 && x != 1 && x !=8)
{
x = x-1;
iTotalMoves++;
}
}while(x!= 1 && x!= 8);
if(x==1)
{
iTotalHome++;
}
if(x==8)
{
iTotalPub++;
}
}
fAverageMoves = iTotalMoves/1000000;
fAverageHome = iTotalHome/1000000;
cout << "I = " << N << endl;
cout << "Average Moves:" << fAverageMoves<< endl;
cout << "Total Home:" << iTotalHome<< endl;
cout << "Average Home:" << fAverageHome << endl;
cout << "Total Moves:" << iTotalMoves<< endl<< endl;
N++;
}while(2<=N && 7>=N);
return;
```

0

ok, im closer im gettin roughly desireable answers but for every step its just repeating the same as the last. its not changing at all

0

yea. i cant get my loop right to make it ontinue to add to the variables.

0

```
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int N;
float iTotalHome, iTotalMoves, iProbHome, x;
float fAverageMoves, fAverageHome;
void Walk(float &fAverageMoves, float &iTotalMoves, float &iTotalHome,
float &fAverageHome, int &N, float &x, float &iProbHome);
srand(time(0));
Walk(fAverageMoves, iTotalMoves, iTotalHome, fAverageHome, N, x,
iProbHome);
return 0;
}
void Walk(float &fAverageMoves, float &iTotalMoves, float &iTotalHome, float
&fAverageHome, int &N, float &x, float &iProbHome)
{
int T;
do
{
cout<< "Please enter the block number starting with 2 and ending with 7. One"<< endl;
cout << " at a time." << endl;
cin >> N;
x=N;
for(T=1000000; 0<=T; T--)
{
iProbHome = rand()%3;
if(iProbHome == 1 || iProbHome == 2 && x!=1 && x!=8)
{
x++;
iTotalMoves++;
}
if(iProbHome ==0&& x!= 1 && x!= 8)
{
x--;
iTotalMoves++;
}
if(x==1)
iTotalHome++;
iProbHome=0;
}
fAverageHome=iTotalHome/1000000;
fAverageMoves= iTotalMoves/1000000;
cout<< "I = " << N << endl;
cout<< "Total Moves = " << iTotalMoves << endl;
cout<< "Average Moves = " << fAverageMoves << endl;
cout<< "Total Home = " << iTotalHome << endl;
cout<< "AverageHome = " << fAverageHome << endl;
}while(2<=N);
return;
}
```

thats my most updated code so far.

but heres what its giving me.

Please enter the block number starting with 2 and ending with 7. One

at a time.

2

I = 2

Total Moves = 999996

Average Moves = 0.999996

Total Home = 1

AverageHome = 1e-06

Please enter the block number starting with 2 and ending with 7. One

at a time.

3

I = 3

Total Moves = 1.99999e+06

Average Moves = 1.99999

Total Home = 4

AverageHome = 4e-06

Please enter the block number starting with 2 and ending with 7. One

at a time.

4

I = 4

Total Moves = 2.99999e+06

Average Moves = 2.99999

Total Home = 4

AverageHome = 4e-06

Please enter the block number starting with 2 and ending with 7. One

at a time.

5

I = 5

Total Moves = 3.99999e+06

Average Moves = 3.99999

Total Home = 4

AverageHome = 4e-06

Please enter the block number starting with 2 and ending with 7. One

at a time.

6

I = 6

Total Moves = 4.99999e+06

Average Moves = 4.99999

Total Home = 4

AverageHome = 4e-06

Please enter the block number starting with 2 and ending with 7. One

at a time.

7

I = 7

Total Moves = 6e+06

Average Moves = 6

Total Home = 4

AverageHome = 4e-06

Please enter the block number starting with 2 and ending with 7. One

at a time.

^X exit

I = 7

Total Moves = 6.99999e+06

Average Moves = 6.99999

Total Home = 4

AverageHome = 4e-06

Please enter the block number starting with 2 and ending with 7. One

at a time.

plz plz plz help... ive been working on this for hours and im sooo lost

0

There's a number of major problems with your code.

I'd highly recommend you to redesign it according the following guidelines:

1. Create a function `int make_step(int prob)`

returning either 1 or -1. Try to understand what `rand()`

does, and how to calculate directions given the probability of walking to the left (so far you don't seem to understand it).

2. Create a function `walk(int startpos)`

returning single walk data. It should continuously invoke `make_step`

to update a position until either a home or a pub is reached.

3. Create a function `void simulate(int startpos)`

which will call `walk`

for 100000 times accumulating the data, and print the statistics.

4. Call `simulate`

for startpos from 2 to 7.

0

```
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int N, iTotalMoves;
int iTotalHome=0;
int iTotalPub=0;
int x, iProbHome;
float fAverageMoves, fAverageHome;
int Prob(int &iProbHome);
int StartPos(int &N, int &x, void Walk(int x, int iProbHome, int &iTotalMoves,
int &iTotalHome, int &iTotalPub, int Prob(int &iProbHome), void Print(float &fAverageMoves,
float &fAveragHome, int iTotalMoves, int iTotalHome)));
void Walk(int iProbHome, int x, int &iTotalMoves, int &iTotalHome,
int &iTotalPub, float &fAverageMoves, float &fAverageHome, int Prob(int &iProbHome),
void Print(float &fAverageMoves, float &fAverageHome, int iTotalMoves, int iTotalHome));
void Print(float &fAverageMoves, float &fAverageHome, int iTotalMoves, int iTotalHome);
srand(time(0));
StartPos(N, x, Walk(N, iProbHome, x, iTotalMoves, iTotalHome, iTotalPub, fAverageMoves, fAverageHome,
Prob(iProbHome)));
return 0;
}
int StartPos(int &N, int &x, void Walk(int x, int iProbHome, int &iTotalMoves, int &iTotalHome,
int &iTotalPub, int Prob(int &iProbHome), void Print(float &fAverageMoves, float &fAverageHome,
int iTotalMoves, int iTotalHome)))
{
cout<< "Please enter a number between two and seven, starting with two" <<endl;
<< "and going up by a single integer until 7 has been inputed." << endl;
cin >> N;
x=N;
if(2<=N&& N<=7)
{
Walk(x, iProbHome, iTotalMoves, iTotalHome, iTotalPub, fAverageMoves, fAverageHome, Prob(iProbHome),
Print(fAverageMoves, fAverageHome, iTotalMoves, iTotalHome) );
}
return N;
}
int Prob(int &iProbHome)
{
iProbHome = rand()%6;
cout<< iProbHome<< endl;
return iProbHome;
}
void Walk(int iProbHome, int x, int &iTotalMoves, int &iTotalHome, int &iTotalPub,
int Prob(int &iProbHome), void Print(float &fAverageMoves, float &fAverageHome,
int iTotalMoves, int iTotalHome))
{
int T;
T=1000000;
do
{
Prob(iProbHome);
if(iProbHome == 1 || iProbHome == 2 || iProbHome == 3 || iProbHome ==5)
{
x=x+1;
iTotalMoves++;
}
if(iProbHome == 0 || iProbHome == 4)
{
x=x-1;
iTotalMoves++;
}
if(x==1)
{
iTotalHome++;
}
if(x==8)
{
iTotalPub++;
}
T--;
}while(T>= 0);
Print(fAverageMoves, fAverageHome, iTotalMoves, iTotalHome);
}
void Print(float &fAverageMoves, float &fAverageHome, int iTotalMoves, int iTotalHome)
{
fAverageMoves= iTotalMoves/1000000;
fAverageHome = iTotalHome/1000000;
cout << "I = " << N << endl;
cout << "Average Moves = " << fAverageMoves << endl;
cout << "Total Moves = " << iTotalMoves << endl;
cout << "AverageHome = " << fAverageHome << endl;
cout << "TotalHome = " << iTotalHome << endl;
return;
}
bash-3.2$ ext
bash: ext: command not found
bash-3.2$ exit
exit
Script done on Thu 17 Dec 2009 01:44:25 PM CST
```

**this is what i have now. but im having problems with the functions. am i declaring them wrong?**

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...