-
November 29th, 2011, 11:07 PM
#1
Two newbie questions regarding the vector class
Hey all,
Just for fun, I've decided to try to design a card game. However, as I'm pretty new at this, I'm taking baby steps. Anyways, the problem I have right now is how to generate 5 unique, random numbers from a predefined array.
Someone suggested that I check out the vector class and it seems to do what I want rather nicely. Right now, I am using random_shuffle() to randomly sort the elements of a predefined array. Here's my code:
Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> cards(5);
cards[0]=1;
cards[1]=2;
cards[2]=3;
cards[3]=4;
cards[4]=5;
random_shuffle (cards.begin(), cards.end());
int na_playerhand[5];
for (int i=0; i<5, i++)
{
na_playerhand[i]=cards[i];
cout << playerhand[i]'
}
return 0;
}
While this code works great the first time around (i.e. it outputs the numbers 1-5 in random order, for example "35421"), for each subsequent time I run the program, I get the same sequence of numbers (i.e. the output is always "35421")---what gives?
As for the second question, my initialization of the array seems rather cumbersome, is there some kind of shortcut to initializing a vector array?
Thanks!
-
November 29th, 2011, 11:10 PM
#2
Re: Two newbie questions regarding the vector class
You did not seed the pseudorandom number generator that random_shuffle uses, so it used the default seed on each run of the program. Hence, use srand at the start of the main function, possibly with a seed derived from current time or some random source.
-
November 30th, 2011, 08:22 AM
#3
Re: Two newbie questions regarding the vector class
Originally Posted by Ulnarian
As for the second question, my initialization of the array seems rather cumbersome, is there some kind of shortcut to initializing a vector array?
You may use a loop to initialize your vector. Either construct the vector with the desired number of items as you do now and then write each item in a loop, or default-construct the vector (i.e. without passing the number of items upon construction) and then add the items in the loop using the vector's push_back() member. Personaly, I prefer the second approach. While it actually may be a bit less efficient performance-wise, it's easier to write, IMO more elegant and safer.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 30th, 2011, 10:49 AM
#4
Re: Two newbie questions regarding the vector class
Originally Posted by Eri523
You may use a loop to initialize your vector. Either construct the vector with the desired number of items as you do now and then write each item in a loop, or default-construct the vector (i.e. without passing the number of items upon construction) and then add the items in the loop using the vector's push_back() member.
Another way is to use Boost.Iterator's counting_iterator.
-
November 30th, 2011, 02:50 PM
#5
Re: Two newbie questions regarding the vector class
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 30th, 2011, 03:11 PM
#6
Re: Two newbie questions regarding the vector class
@Eri523:
You can replace the output loop:
Code:
for (int i = 0; i < 5; ++i)
std::cout << cards[i] << ' ';
with
Code:
std::copy(cards.begin(), cards.end(), std::ostream_iterator<int>(cout, " "));
-
November 30th, 2011, 03:22 PM
#7
Re: Two newbie questions regarding the vector class
Thanks for adding that Peter_B. That's exactly what I was too lazy to look up. Actually, I come across practically that same line every once in a while and still haven't managed to properly memorize it...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|