Clearing the cache for a random number?

Hi all,

I currently have the code;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
               cout <<"//Computer's turn!" << endl;
computerPick = rand () % 1 + 25;

system ("PAUSE");

if (computerPick == placePatrol || computerPick == placeAirCarr || computerPick == placeBatt || 
computerPick == placeDest || computerPick == placeSub && computerPick < 25)
{
                cout <<"//Computer hits!" << endl;
                computerShipCount--;
}

else
                cout <<"//Computer misses!" << endl;

system ("PAUSE");

}


The emboldened phrase is what I'm specifically referring to in my title. That generates a random number, but the problem is every time the code goes through, it will keep the exact same number which is not what I want. I've tried to use a pointer but I must have been doing something wrong.
So I'm asking if it's possible to have some sort of code that either wipes the cache of computerPick or that can generate a new number each time the code goes round.

Many thanks,
Well, what did you expect? All integers are evenly divisible by 1, so the remainder is always 0 and computerPick will always be 25.
Oh. I see. I used this:
1
2
3
4
5
compAirCarr = rand () % 25 + 25;
compBatt = rand () % 25 + 25;
compDest = rand () % 25 + 25;
compSub = rand () % 25 + 25;
compPatrol = rand () % 25 + 25;

earlier on in my program which generates random numbers between 25 and 50...I assumed I could just switch it to "1 + 25" and I'd have a number between 1 and 24.
That's not quite how it works :P

Basically, rand() generates a random integer.
rand() % 25 gives you the remainder of dividing this random number by 25, i.e. some number between (inclusive) 0 and 24.

So they if you want a random number between 25 and 49 (inclusive), you just add 25 to it:
rand() % 25 + 25

Your code:
rand() % 1 + 25
Finds the remainder of the random number following division by 1 and adds 25. As Athar said, the remainder of division of any integer by 1 is 0, so this expression always returns 0 + 25 = 25.

Hope this helps. Have you worked out what you need to do to get what you want now?
@OP: You can do that, but you have to write it correctly compAirCarr = rand() % 25 + 1; See the difference? Order of operations is something that is easy to forget about, which is why you should always try to remember it.

That's not quite how it works :P

Basically, rand() generates a random integer.
rand() % 25 gives you the remainder of dividing this random number by 25, i.e. some number between (inclusive) 0 and 24.

So they if you want a random number between 25 and 49 (inclusive), you just add 25 to it:
rand() % 25 + 25

Your code:
rand() % 1 + 25
Finds the remainder of the random number following division by 1 and adds 25. As Athar said, the remainder of division of any integer by 1 is 0, so this expression always returns 0 + 25 = 25.

Hope this helps. Have you worked out what you need to do to get what you want now?


Good thing I posted this in the beginners section then isn't it :P I read somewhere that the whole
25 + 25 thing was a scope...I just blindly went along with it :P but yes, thank you :)


@OP: You can do that, but you have to write it correctly compAirCarr = rand() % 25 + 1; See the difference? Order of operations is something that is easy to forget about, which is why you should always try to remember it.


Yes I do, thank you very much :)









Is there any way to make it so that
computerPick

becomes a different random number every time?

I'm thinking I'll need some sort of accumulator array for it that compares the value input with the previous values? I'm not too sure how I'd go about it though.
I'm thinking I'll need some sort of accumulator array for it that compares the value input with the previous values? I'm not too sure how I'd go about it though.

Yes, the accumulator array is called std::vector:
http://www.cplusplus.com/reference/stl/vector/

Alternatively, you can pregenerate the numbers by pushing 1-25 into a vector and then use random_shuffle on it, as shown here in the example:
http://www.cplusplus.com/reference/algorithm/random_shuffle/

When you run out of numbers, you'll have to refill the vector.
The shuffle makes more sense to me, but how can I use pointers to bring up the first value in the address?
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
// random_shuffle example
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;

// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}

// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;

int main () {
  srand ( unsigned ( time (NULL) ) );
  vector<int> myvector;
  vector<int>::iterator it;

  // set some values:
  for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

  // using built-in random generator:
  random_shuffle ( myvector.begin(), myvector.end() );

  // using myrandom:
  random_shuffle ( myvector.begin(), myvector.end(), p_myrandom);

  // print out content:
  cout << "myvector contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;

  cout << endl;

  return 0;
}


I obviously know that I need to get rid of the printing bit to stop the user seeing the values, but I can't just use something like this

1
2
3
4
5

int x;
...
cout << *myrandom[x]; << endl;
x++;


can I?
Topic archived. No new replies allowed.