0

Im trying to write a code for a class that has 20 activities .. each activity has a start time and a finish time.

When i try to generate the times the start time is larger than the finish in some cases. can any one help me with my function error.. im trying to get all the start times random other than 0 and smaller than the finish and pass to my set and get funtions in main.

int randomStart(int x)
{
	int a;
	int q;
	x= 0+rand()%(69-0)+1;
	q = randomFinish(a);
	while(x>q)
		{
		x= rand()%69;
		q = randomFinish(a);
		//return x;
	}
		
	return x;
	
	
}

int randomFinish(int y)
{
	y = 1 + rand()%70;
	return y;
}

Edited by __avd: Added [code] tags.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

Your code is confusing. I'm assuming you assign a value to "a" in code you haven't shown, otherwise you're getting whatever junk is in "a" to begin with.

Why not just use rand() to generate a start time, then call it again to generate a random offset from the start time? Afterwards, add that offset to your start time to get the finish time.

Also please use [code] //code here [/code] code tags next time.

Edited by jonsca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.