-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleTraveralRandom.h
77 lines (53 loc) · 1.08 KB
/
SingleTraveralRandom.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* RandomTools.h
*
* Created on: 2013Äê11ÔÂ25ÈÕ
* Author: houzhi
*/
#ifndef RANDOMTOOLS_H_
#define RANDOMTOOLS_H_
#include<time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
class SingleTraveralRandom {
public:
SingleTraveralRandom(int select_nums);
int SingleTraversal();
void Init();
private:
int Random(int max);
size_t cur_index;
//Ñ¡Ôñ¶àÉÙ
size_t select_nums;
};
SingleTraveralRandom::SingleTraveralRandom(int select_nums){
this-> select_nums = select_nums;
cur_index = 0;
}
inline int SingleTraveralRandom::Random(int max){
srand((unsigned )time(NULL));
int res = rand() % max;
return res;
}
/**
* ³õʼ»¯
*/
inline void SingleTraveralRandom::Init(){
cur_index = 0;
}
int SingleTraveralRandom::SingleTraversal(){
cur_index ++;
if(cur_index <= select_nums ){
return cur_index-1;
}else{
int random = Random(cur_index);
if(random <=select_nums){
return random-1;
}else{
return -1;
}
}
}
#endif /* RANDOMTOOLS_H_ */