Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: Random arra...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 11 Topic 4058 of 4306
Post > Topic >>

Re: Random array of unique numbers

by "Jim Langston" <tazmaster@[EMAIL PROTECTED] > Mar 7, 2008 at 11:10 PM

Will wrote:
> Hi, I'm trying to take the value of elements in one array and scramble
> them up so that a new array contains a random version of the first
> with out duplicates. I thought the following code would do but I'm not
> geeting the second array filled with the first ones elements.
>
> #include <iostream.h>
> #include <time.h>
> void main()
> {
> int i = 0, j = 0;
> int intSeq[7] = {1,2,3,4,5,6,7};
> int intRand[7] = {0,0,0,0,0,0,0};
>
> for (i = 0; i <= 6; i++)
> {
> while(intRand[j] == 0)
> {
> srand(time(NULL));
> j = random(7);
> intRand[j] = intSeq[i];
> }
> }
> /*From here on is code for output to display*/
> cout << "intSeq[] = ";
> for (i = 0; i <= 6; i++)
> {
> cout << intSeq[i] << " ";
> }
> cout << endl;
> cout << "intRand[] = ";
> for (i = 0; i <= 6; i++)
> {
> cout << intRand[i] << " ";
> }
> cout << endl << endl;
>
> system("pause");//Function for closing salutation.
> }
>
> The output I get from my code shows assignment of one element only,
> does anyone know where I'm going wrong or have an easy scramble
> algotithm?
> Many thanks for reading, Will.

Here is a more C++ish version of the program that works.

#include <iostream>
#include <ctime>
#include <algorithm>

int main()
{
    int intSeq[7] = {1,2,3,4,5,6,7};

    srand(time(NULL));
    std::random_shuffle( intSeq, intSeq + 7 );

    std::cout << "intSeq[] = ";
    for ( std::size_t i = 0; i <= 6; i++)
    {
        std::cout << intSeq[i] << " ";
    }
    std::cout << std::endl;

    system("pause");//Function for closing salutation.
}

What you are attempting to write is a shuffle.  Shuffles can be a little 
difficult because using rand returns a number from 0 to RAND_MAX which may

be something like 32767 or higher.  Then you need to convert this to a 
unique number from 0 to 6 which is difficult, how do you make sure you 
didn't duplicate a number, etc?

Well, the standard template library already has it for you, located in 
<algorithm> it is called random_shuffle.  You give it the beginning
iterator 
(in this case using a simple pointer) and ending interator.  Note that the

ending iterator is one PAST the last element.

Other notes: <istream.h> is obsolete, use <iostream>, <time.h> a c header 
can be found moved to the stl with <ctime>  most (all?) of the standard C 
headers can be found by removeing the .h extention and adding a c in
front. 
<math.h> becomes <cmath> etc...

I could make this more C++ish by using a std::vector, although vectors can

be a bit of a pain to initialize.  My understanding is that the next
version 
of C++ will allow vectors to be initilaized with a list (if not they 
definately need to be) but currently the program would become:

#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> intSeq;
    for ( int i = 0; i < 7; ++i )
    {
        intSeq.push_back( i );
    }

    srand(time(NULL));
    std::random_shuffle( intSeq.begin(), intSeq.end() );

    std::cout << "intSeq[] = ";
    for ( std::size_t i = 0; i <= 6; i++)
    {
        std::cout << intSeq[i] << " ";
    }
    std::cout << std::endl;

    system("pause");//Function for closing salutation.
}

Now, for the output we can make it more C++ish, one thing we could chose
to 
do is instead of using a count use the size of the vector for the output:

    std::cout << "intSeq[] = ";
    for ( std::vector<int>::const_iterator it = intSeq.begin(); it != 
intSeq.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

Now, we can even go farther than this.  Instead of iterating through the 
vector and using std::cout << for each element, we can use std::copy to do

it.

    std::cout << "intSeq[] = ";
    std::copy( intSeq.begin(), intSeq.end(), 
std::ostream_iterator<int>(std::cout, " ") );
    std::cout << std::endl;

although now we're getting into the black magic part of C++ where it is a 
more advanced topic, but good to learn from the start.

Lastly, you have the system("pause"); in there because you are using 
Microsoft Visual C++ and you are running the program with F5 and the
window 
closes before you can see the output.  Instead of adding the 
system("pause"); just use ctrl-F5 when you run the program and it will
pause 
for you.  So the program becomes:

#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> intSeq;
    for ( int i = 0; i < 7; ++i )
    {
        intSeq.push_back( i );
    }

    srand(time(NULL));
    std::random_shuffle( intSeq.begin(), intSeq.end() );

    std::cout << "intSeq[] = ";
    std::copy( intSeq.begin(), intSeq.end(), 
std::ostream_iterator<int>(std::cout, " ") );
    std::cout << std::endl;

}

-- 
Jim Langston
tazmaster@[EMAIL PROTECTED]

 




 11 Posts in Topic:
Random array of unique numbers
Will <wburchell@[EMAIL  2008-03-07 19:29:36 
Re: Random array of unique numbers
"osmium" <r1  2008-03-07 11:41:47 
Re: Random array of unique numbers
Francis Glassborow <fr  2008-03-08 00:24:16 
Re: Random array of unique numbers
Will <wburchell@[EMAIL  2008-03-08 11:47:19 
Re: Random array of unique numbers
Ian Collins <ian-news@  2008-03-08 08:45:47 
Re: Random array of unique numbers
"Jim Langston"   2008-03-07 23:10:35 
Re: Random array of unique numbers
Richard Heathfield <rj  2008-03-08 09:17:25 
Re: Random array of unique numbers
Will <wburchell@[EMAIL  2008-03-08 12:06:07 
Re: Random array of unique numbers
Francis Glassborow <fr  2008-03-08 12:28:18 
Re: Random array of unique numbers
Francis Glassborow <fr  2008-03-08 12:25:33 
Re: Random array of unique numbers
stan <smoore@[EMAIL PR  2008-03-08 15:45:52 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 12:09:44 CDT 2008.