Arrange elements of vectors randomly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akinidu
    New Member
    • May 2010
    • 7

    Arrange elements of vectors randomly

    Suppose i have a vector a[i] having size of100, which means i=100 here ! Now I know if my vector is random i can rearrange it using qsort() in C..or can use bubble sort algorithm !

    But suppose I want to arrange my elements randomly and remove 30% of numbers from it and again rearrange it using indices !

    Problem :

    How to arrange a elements of vector a[i] where i is = to 100, in random manner then chop off 30% elements (randomly) then rearrange the indices now i = 70 in the same order !

    Thanks

    kk
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    You say the vector elements are in random order.
    You can use erase() or pop_back()to discard the values of 30 elements. The remaining elements will still be in a form of random order but having discarded 30 values it is not possible to get the order of the 70 values identical to the order of the original 100 values.i.e.70 will not compare with 100

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Yo randomise your vector you can do a shuffle. A shuffle is where you randomly choose 2 indexes and then swap the values at those 2 indexes and repeat that step a few 100 times.

      Once done chop off the last 30 elements and the rest of the vector will still be in the same order.

      Comment

      Working...