#include <newran.h>
Inheritance diagram for RandomPermutation:
Public Member Functions | |
void | Next (int N, int M, int p[], int start=0) |
void | Next (int N, int p[], int start=0) |
Private Attributes | |
Random | U |
|
Reimplemented in RandomCombination. Definition at line 656 of file newran.h.
00656 { Next(N, N, p, start); } |
|
Reimplemented in RandomCombination. Definition at line 927 of file newran.cpp. References ErrorNoSpace(), Ars::Graf::M, Random::Next(), and U. Referenced by RandomCombination::Next(), and test4().
00928 { 00929 // N = size of urn; M = number of draws 00930 if (N < M) Throw(Logic_error("Newran: N < M in RandomPermutation")); 00931 int i; 00932 int* q = new int [N]; 00933 if (!q) ErrorNoSpace(); 00934 for (i = 0; i < N; i++) q[i] = start + i; 00935 for (i = 0; i < M; i++) 00936 { 00937 int k = i + (int)(U.Next() * (N - i)); // uniform on i ... N-1 00938 p[i] = q[k]; q[k] = q[i]; // swap i and k terms 00939 // but put i-th term into p 00940 } 00941 delete [] q; 00942 } |
|
Definition at line 649 of file newran.h. Referenced by Next(). |