File: util-random.H

package info (click to toggle)
bali-phy 3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,608 kB
  • sloc: cpp: 67,094; xml: 4,074; perl: 3,715; haskell: 1,861; yacc: 1,067; python: 555; lex: 528; sh: 259; makefile: 20
file content (51 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
   Copyright (C) 2004,2007 Benjamin Redelings

This file is part of BAli-Phy.

BAli-Phy is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

BAli-Phy is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with BAli-Phy; see the file COPYING.  If not see
<http://www.gnu.org/licenses/>.  */

#include <vector>
#include "util/assert.hh"
#include "rng.H"

template <typename T>
void random_shuffle(std::vector<T>& v) 
{
  for(int i=1;i<v.size();i++) 
  {
    int l = myrandom(i+1);
    if (l<i)
      std::swap(v[l],v[i]);
  }
}

template <typename T>
std::vector<T> randomize(std::vector<T> v) 
{
  random_shuffle(v);
  return v;
}

template <typename T>
std::vector<T> sort_and_randomize(std::vector<T> v) 
{
  std::sort(v.begin(), v.end());
  random_shuffle(v);
  return v;
}


std::vector<int> random_permutation(int size);