rand() and if statement

Hello all,

I'm writing a piece of program where I use '1 + rand() % 10' to generate numbers between 1 and 10. Rand() function is followed by an if and if else statements.

The program should output different incremented values every second, but is instead only incrementing as per the first if condition (by 3). ONLY the first if statement is executed, else if and else are NEVER considered. Even when random number fails the test, the first if statement executes despite that.

Why? It all starts and stops at the first if condition.

I have tried to debug with inserting 'cout' and also with 'break points' in gdb debugger. Couldn't see the nitty gritty details enough to find the bug.

Thanks!
Matt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// function prototypes
int moveTortoise(int, int);
void clockTick();


int main()
{
   int prev;
   int tortoise_position = 0;

   srand(time(0));
   int random = 1+ rand() % 10;
   

   while(tortoise_position <= 69) // ****************************  LOOP  ***********************
   {
      // call moveTortoise()

      tortoise_position = moveTortoise(random, prev);
      cout << "tortoise_position: " << tortoise_position << endl;

      prev = tortoise_position;
   } // ************************  END LOOP  *****************************

return 0;
}


int moveTortoise(int r, int previous)
{
   // clockTick creates one second delay. leave it here!!
   clockTick();

   if(1 <= r <= 5)
   {
      return previous + 3;
   }
   else if(6 <= r <= 7)
   {
      return previous - 6;
   }
   else
   {
      return previous + 1;
   }
}


void clockTick()
{
   clock_t second;      // generate a variable 'second' that holds 1 second
   second = clock() + 1*CLOCKS_PER_SEC;   // change 1 if you want a different delay
   while(clock() < second) {};   // empty loop
}
Last edited on
Could it be that:

1
2
3
4
5
6
7
8
9
10
11
12
   if(1 <= r <= 5)
   {
      return previous + 3;
   }
   else if(6 <= r <= 7)
   {
      return previous - 6;
   }
   else
   {
      return previous + 1;
   }


Should be:

1
2
3
4
5
6
7
8
9
   if(1 <= r && r <= 5)
   {
      return previous + 3;
   }
   if(6 <= r && r <= 7)
   {
      return previous - 6;
   }
   return previous + 1;


It might be that (1 <= r) condition is only seen and all the numbers fulfill this. Also no point in the else statements as you have a return in each case

Hope that helped :)
Last edited on
replace
if(1 <= r <= 5)
with
if(1 <= r && r <= 5)

I believe that first line will evaluate like this:
1
2
if(1 <= r <= 5)
if(true   <= 5)
Awesome!!

Thank you, guys. You were both right. The true/false condition wasn't formed correctly. && solved it.
Now it's working.

charij, thanks for the note about not needing else if/else.

Matt
Last edited on
Topic archived. No new replies allowed.