Changeset 125


Ignore:
Timestamp:
Aug 2, 2004, 5:15:34 PM (17 years ago)
Author:
Peter
Message:

shuffle function added

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/vector.cc

    r109 r125  
    11// $Id$
     2
     3#include "vector.h"
     4#include "random_singleton.h"
    25
    36#include <iostream>
     
    710#include <utility>
    811
    9 #include "vector.h"
     12
     13
    1014
    1115
     
    170174  }
    171175
    172 
     176  vector vector::shuffle(vector& vec) const
     177  {
     178    vector tmp_vec(vec);
     179    theplu::cpptools::random_singleton*
     180      rnd=theplu::cpptools::random_singleton::get_instance();
     181    for (size_t i=0; i<vec.size(); i++){
     182      size_t index = rnd->get_uniform_int(vec.size()-i);
     183      vec(i)=tmp_vec(index);
     184      tmp_vec(index)=tmp_vec(vec.size()-i-1);
     185    }
     186   
     187    return vec;
     188  }
    173189
    174190  double vector::sum(void) const
  • trunk/src/vector.h

    r109 r125  
    138138
    139139    ///
     140    /// Randomly shuffles the elements in vector @return resulting
     141    /// vector
     142    ///
     143    vector vector::shuffle(vector&) const;
     144
     145    ///
    140146    /// @return the number of elements in the vector.
    141147    ///
Note: See TracChangeset for help on using the changeset viewer.