Thread: Random Sequences

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    14

    Random Sequences

    I need a way to either:

    a) Put variables or strings in a random order, i.e. 10 strings containing names of people, output instance could be like this:

    George
    Bob
    Zac
    Pat
    Peter

    and then another isntance:

    Bob
    Zac
    Pat
    George
    Peter

    etc.

    or

    b) Generate a random sequence of numbers, i.e. generate 10 DIFFERENT random numbers. an instance of output:

    1
    7
    8
    9
    2
    3
    6
    5
    4
    10

    and then

    10
    1
    8
    9
    6
    4
    5
    3
    2
    7

    etc.
    yo

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You want a random permutation. Just fill the array with the values you want, then shuffle it randomly:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
      int random[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      int N = 10;
      
      for ( int i = 0; i < N - 1; i++ ) {
        int r = (double)rand() / RAND_MAX * ( N - i );
        int save = random[i];
        random[i] = random[i + r];
        random[i + r] = save;
      }
      for ( int i = 0; i < N; i++ )
        cout<< random[i] <<' ';
      cout<<endl;
    }
    My best code is written with the delete key.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You don't need us to tell you how to output random numbers. You need to do your own homework. If you're having difficulty with the code you've written, that's one thing, but you can't just post your assignment and expect us to do your work for you.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    I did write code. It didn't work, and I don't know why. It's so jumbled I didn't want to post it, and it's not an assignment. I'm just doing this because I want to.
    yo

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by zowen
    I did write code. It didn't work, and I don't know why. It's so jumbled I didn't want to post it, and it's not an assignment. I'm just doing this because I want to.
    Thats OK. I'd suggest reworking your own code, and posting it here when it doesn't work. You'll learn a lot that way.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Random Sequences

    So look up srand() and rand() to get it done.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A handy way to deal with things of this nature is with the next_permutation function defined in the <algorithm> header. Store your names in a container, such as a vector and the next_permutation function can go through the container and continuously switch around the order of the elements in the container until there are no more permutations to be found. The only thing is that to make sure you get all the possible permutations the elements must exist already ordered (least to greatest) in the container before you start running the permutations on them. As an example, this will write all the various permutations of the names to a file called List.Txt, five names seperated by spaces in different order in each line of the file. With 5 names there are 120 total permutations so there will be 120 lines in the file.
    Code:
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        vector<string> StrVect;
        ofstream Output("List.Txt");
    
        // Add elements to the container
    
        StrVect.push_back("George");
        StrVect.push_back("Bob");
        StrVect.push_back("Zac");
        StrVect.push_back("Pat");
        StrVect.push_back("Peter");
    
        // Make sure elements are first sorted
    
        sort( StrVect.begin(), StrVect.end() );
    
        // Write out all permutations to a file
    
        do
        {
            copy( StrVect.begin(), StrVect.end(), ostream_iterator<string>(Output," ") );
            Output << endl;
        } while( next_permutation(StrVect.begin(),StrVect.end()) );
    
        return 0;
    }
    If you have 10 items then there are 10! or 3628800 possible permutations and you would probably not want all that displayed to the screen or even a file so you would not want to do things exactly as I have done them. You seem to be saying you only want a couple of those possible 3628800 permutations displayed so you would need to either modify this (easily done) or choose another way to go. And of course this could be easily adapted to using a container of int values instead of the string values.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Look up random_shuffle(), it's part of the standard library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM