Randomly selecting objects in C++


Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Randomly selecting objects in C++

Hybrid View

  1. #1
    Join Date
    Aug 2000
    Posts
    116

    Randomly selecting objects in C++

    I need to be able to randomly select an object in C++ for a program I'm writing. Here's an example of what I'm trying to do:
    Code:
    class person
    {
       int weight;
       int height;
       int age;
    };
    
    person bob, mary, john;
    I want my program to randomly pick one of the three objects (bob, mary, or john) and list all of that person's stats (weight, height, and age). What's the easiest way to go about doing this? I'm sure I could make a huge and complex "switch" statement or a bunch of "if" commands, but there has to be an easier way than that.

  2. #2
    Join Date
    Feb 2000
    Location
    cincinnati, oh, usa
    Posts
    918
    If you know how many there will be at compile time, put them in an array and use a random number from 0 to the length of the array - 1 to index into a random element of that array. If you don't know how many there will be, keep them in a list and wait until I get home for the method of choosing a random element from a list

    Jeremy
    --
    X-No-Archive: yes
    I'm working on a Python IRC bot.

  3. #3
    Join Date
    Aug 2000
    Posts
    116
    Hmm, I'm not much of a C++ expert but I think I kinda get what you're saying. But how exactly do put an object into an arrary?

  4. #4
    Join Date
    Mar 2000
    Posts
    427
    something like:

    person people[3];

  5. #5
    Join Date
    Aug 2000
    Posts
    116
    Maybe I'm just dumb but I still don't get it.

  6. #6
    Join Date
    Mar 2000
    Posts
    427
    Code:
    class person {
    	int age;
    	int weight;
    	int height;
    };
    
    // one object
    person joe;
    
    // array of objects
    person people[3];
    
    // set age of single object
    joe.age = 5;
    
    // set age of two of the objects in the three object array
    people[0].age = 15;
    people[1].age = 80;
    
    // or to do it randomly
    
    people[some_random_number].age = 14;

  7. #7
    Join Date
    Jan 2000
    Location
    United States
    Posts
    943
    Originally posted by Xprotocol:
    <STRONG>Maybe I'm just dumb but I still don't get it. </STRONG>
    I hate to RTFM, but in this situation, I don't really feel a tinge of guilt. Go continue reading up on your C++.
    All four star people are not cool!!

    Wait a minute, that includes me, doesn't it? Damn...

    Debian monks aren't slackers!

    Previous nick names:
    ndogg, Muad Dib --formerly known as ndogg

  8. #8
    Join Date
    Aug 2000
    Posts
    116
    Ic now, thnx binary. And Muad Dib, I already have RTFM, many of them actually. Sadly, this problem of mine is not explained in any of them, I checked. I usually try to RTFM before I ask for help.

  9. #9
    Join Date
    May 2000
    Location
    Tulsa, Ok, USA
    Posts
    1,580
    Xprotocol:

    You could also write a class for handling the actual array of people. This handling class could be also set up to do the random item selection, so the main loop of the program would only look as:
    Code:
    int main(void)
    {
      class PersonHandler people;
      //
      people.add("bob",178,72,17);
      people.add("mary",135,60,31);
      people.add("john",230,70,35);
      //
      people.showRandom();
      return 0;
    }
    It would take a little bit of work to set this up, but it would certainly make the usage of the person class a little more friendly, so the user isn't required to do so much in the main part of the code.

    I'm not as familiar with c++ as I am java, so I can provide a java example of this with ease, but a c++ example would take me a little bit of time to research. (^=
    Ephesians 2:8-9

  10. #10
    Join Date
    Mar 1999
    Posts
    242
    Use a vector instead of an array. You get all the good of an array, and it's expandable, so you can dynamically change the number of people at run time, and add new ones, remove old ones, et cetera. Not as effieient as a list for insertion/deletion, but oh so much better for traversage, and it would work fine for choosing one randomly.

    [ 09 July 2001: Message edited by: Bradmont ]

  11. #11
    Join Date
    Feb 2000
    Location
    cincinnati, oh, usa
    Posts
    918
    /me thinks Xprotocol needs to find a better manual.

    Anyway, I'd use an array of pointers, not an array of instances. Unless there's something I'm missing about C++ (which there could be, since I don't know it ) the array of pointers will be much more memory efficient, and will probably lead to semantics nearer to that which you expect.

    EDIT: In retrospect, it's highly likely that C++ automatically makes arrays of classes/structs into arrays of pointers to those classes or structs, since that's what C does with structs, iirc.

    Jeremy

    [ 09 July 2001: Message edited by: jemfinch ]
    --
    X-No-Archive: yes
    I'm working on a Python IRC bot.

  12. #12
    Join Date
    Aug 2000
    Posts
    116
    I'm just going to stick with the original idea. Its working fine in my program right now. I know its not very professional to make all my data public and that I should use pointers but I think in this situation it would be much easier to go the lazy way . Anyways, thanks for all your help guys.

  13. #13
    Join Date
    May 2000
    Location
    Tulsa, Ok, USA
    Posts
    1,580
    Originally posted by Bradmont:
    <STRONG>Use a vector instead of an array.</STRONG>
    I didn't realize that C++ had vectors.. I guess it reasons to be. I just don't know enough about c++.. maybe from now on I should keep my mouth shut.. (^=
    Ephesians 2:8-9

  14. #14
    Join Date
    Jun 2000
    Location
    Houston, TX, USA
    Posts
    1,290
    TLD had the right idea - if you want to manipulate a group of people, then create a class that handles the operations (whatever they may be). This class would contain a vector, and some lovely functions that could add people, pick random ones, etc. Then the user code would be simple, which is the goal of OOP - hide any necessary ugliness from the user! (the user of the class, you know - not necessarily the computer user).
    "I'm not closed-minded, you're just wrong" - Bucky Katt

  15. #15
    Join Date
    Jul 1999
    Location
    Rochester, NY
    Posts
    1,513
    Originally posted by TheLinuxDuck:
    <STRONG> I didn't realize that C++ had vectors.. I guess it reasons to be. I just don't know enough about c++.. maybe from now on I should keep my mouth shut.. (^=</STRONG>
    hoo boy! ever hear of STL man? STL vectors are nice.
    -kmj- keith - (G)AIM: KMJ2L

    Perl's philosophy is "There's more than one way to do it." I think that Python is one of the best ways.

    Check out codeexamples.org | coderforums.net | The Grasshopper's Journal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •