randomly remove elements

This is a discussion on randomly remove elements within the C++ Programming forums, part of the General Programming Boards category; It seems more difficult than i thought... Ok,another question..Suppose that i have a set( vector) with n elements. How can ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    58

    randomly remove elements

    It seems more difficult than i thought...

    Ok,another question..Suppose that i have a set( vector) with n elements. How can i remove randomly let's say half of them??

    All the remove algorithms of the C++ satisfy a criterion. I don't need one. Just randomly remove some elements.

    Thanx

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I would either generate a random number between 0 and size() - 1 and do this for half of the elements or simply call random_shuffle() and delete size() / 2 elements of the vector. I would probably opt for the latter option.

    Edit: Oh, didn't see the other thread. Oh well, a mod can move my post to the other thread ?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    58
    Ok..thanx a lot.

    Yes, by mistake i double posted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomly rearranging the elements in a vector
    By Signifier in forum C++ Programming
    Replies: 11
    Last Post: 08-01-2007, 01:21 PM
  2. how to remove even elements from list
    By lastrial in forum C Programming
    Replies: 3
    Last Post: 05-17-2007, 11:59 PM
  3. randomly remove elements
    By joan in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 01:46 PM
  4. Remove elements from array
    By Xentor in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2006, 08:47 PM
  5. Replies: 5
    Last Post: 03-06-2006, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21