简体   繁体   中英

How to add elements randomly into a vector using push_back

As the Title states, I've been having trouble with adding random numbers as the elements into a vector of integer type. I included the time and stdlib libraries to work in conjunction with the vector library.

My attempt(using classes) so far has been:

To pass in a vector by reference into a function that adds random elements into a vector. In addition, passing a in reference to a variable I want to update, and using a pointer declared in the class and defined in the constructor to hold the memory address of the variable(same one that is was referenced to in previous lines).

class PlayerHand

{ 
     public:
           playerHand();
           void populateHand(vector<int>&, int&)

     private:
             vector<int> &refVector; 
             int *pPointer;
             int giveCard;
 };

playerHand::playerHand()

{
   srand((time(NULL)));     
   giveCard = (rand() % 14) + 1;

   pPointer = &giveCard;
}


void playerHand::populateHand(vector<int> &refVector, int &refGiveCard)

{ for( int i = 0; i <= 8; i++)
{

    refVector.push_back(*pPointer); //dereference thepointer to store value in giveCard
    srand(time(0));                      
    refGiveCard = (rand() %14) + 1;  /*Here is where I'm trying to update the reference
                                     so before the next loop, the reference should have
                                     a new updated value*/

    cout << refVector.at(i) << " ";
 }
 }

So after I compile the code, the elements in the vector are all the same number, and not different. What mistake in the code am I making?

One problem is that pPointer , giveCard and refGiveCard may not refer to the same memory location. As jogojapan & Karthik pointed out you may also be adding the values to a different vector. You don't show all the code for this so it's hard to tell. Assuming they all do refer to the same memory location and you are adding the values to the correct vector your issue is caused by the call to srand() .

The problem is that you are [almost] always seeding the random number generator with the same value. This is because when you call time(0) it returns the elapsed time in seconds. When you call it repeatedly and very quickly it will return the same value until the clock increments to the next second.

Typically you will call srand() at the beginning of your program or when you really need to reseed the RNG.

Remove the call to srand() from playerHand() and populateHand() and place it in main() and it should take care of you problem.

You are updating refGiveCard , but you are adding giveCard . Therefore they are all the same value.

There is also the fact that you are adding to a different vector than you might expect as jogojapan pointed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM