Skip to content

Commit 7c51b9b

Browse files
committed
Add vector logic Thomas
1 parent 8a6b4b5 commit 7c51b9b

File tree

5 files changed

+285
-8
lines changed

5 files changed

+285
-8
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
*.exe
3+
*.txt

.vscode/settings.json

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"C_Cpp.default.cppStandard": "c++20",
3+
"files.associations": {
4+
"limits": "cpp",
5+
"algorithm": "cpp",
6+
"atomic": "cpp",
7+
"bit": "cpp",
8+
"cctype": "cpp",
9+
"clocale": "cpp",
10+
"cmath": "cpp",
11+
"compare": "cpp",
12+
"concepts": "cpp",
13+
"cstddef": "cpp",
14+
"cstdint": "cpp",
15+
"cstdio": "cpp",
16+
"cstdlib": "cpp",
17+
"cstring": "cpp",
18+
"ctime": "cpp",
19+
"cwchar": "cpp",
20+
"exception": "cpp",
21+
"initializer_list": "cpp",
22+
"ios": "cpp",
23+
"iosfwd": "cpp",
24+
"iostream": "cpp",
25+
"istream": "cpp",
26+
"iterator": "cpp",
27+
"memory": "cpp",
28+
"new": "cpp",
29+
"ostream": "cpp",
30+
"random": "cpp",
31+
"stdexcept": "cpp",
32+
"streambuf": "cpp",
33+
"string": "cpp",
34+
"system_error": "cpp",
35+
"tuple": "cpp",
36+
"type_traits": "cpp",
37+
"typeinfo": "cpp",
38+
"utility": "cpp",
39+
"vector": "cpp",
40+
"xfacet": "cpp",
41+
"xiosbase": "cpp",
42+
"xlocale": "cpp",
43+
"xlocinfo": "cpp",
44+
"xlocnum": "cpp",
45+
"xmemory": "cpp",
46+
"xstddef": "cpp",
47+
"xstring": "cpp",
48+
"xtr1common": "cpp",
49+
"xutility": "cpp",
50+
"iomanip": "cpp",
51+
"charconv": "cpp",
52+
"format": "cpp",
53+
"locale": "cpp",
54+
"mutex": "cpp",
55+
"ratio": "cpp",
56+
"stop_token": "cpp",
57+
"thread": "cpp",
58+
"xlocbuf": "cpp",
59+
"xlocmes": "cpp",
60+
"xlocmon": "cpp",
61+
"xloctime": "cpp"
62+
}
63+
}

.vscode/tasks.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: g++.exe build active file",
6+
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
7+
"args": [
8+
"-fdiagnostics-color=always",
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "${fileDirname}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
},
24+
"detail": "Task generated by Debugger."
25+
},
26+
{
27+
"type": "cppbuild",
28+
"label": "C/C++: release g++.exe",
29+
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
30+
"args": [
31+
"-O2",
32+
"-fdiagnostics-color=always",
33+
"-g",
34+
"${file}",
35+
"-o",
36+
"${fileDirname}\\${fileBasenameNoExtension}_R3.exe"
37+
],
38+
"options": {
39+
"cwd": "${fileDirname}"
40+
},
41+
"problemMatcher": [
42+
"$gcc"
43+
],
44+
"group": {
45+
"kind": "build",
46+
"isDefault": true
47+
},
48+
"detail": "My task release"
49+
}
50+
],
51+
"version": "2.0.0"
52+
}

random_walk.cpp

+29-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <string>
44
#include <random>
55
#include <limits>
6+
#include <algorithm>
7+
68

79
using namespace std;
810

@@ -31,12 +33,14 @@ bool no_need_to_to_continue(vector<int> current_point, int index, int maxIndex)
3133
int main()
3234
{
3335
//int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<int>::max();
34-
int MAX_TRY_BEFORE_GIVEUP = 1000000;
36+
int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<int>::max();;
3537
int DIMENSION = 3;
36-
int NUMBER_OF_TRY = 10000;
37-
cout << "aMAX_TRY_BEFORE_GIVEUP=" << MAX_TRY_BEFORE_GIVEUP << endl;
38+
int NUMBER_OF_TRY = 1000;
39+
int MODULO = 3;
40+
cout << "MAX_TRY_BEFORE_GIVEUP=" << MAX_TRY_BEFORE_GIVEUP << endl;
3841
cout << "DIMENSION=" << DIMENSION << endl;
3942
cout << "NUMBER_OF_TRY=" << NUMBER_OF_TRY << endl;
43+
cout << "MODULO=" << MODULO << endl;
4044

4145
std::random_device rd; // obtain a random number from hardware
4246
std::mt19937 gen(rd()); // seed the generator
@@ -56,6 +60,8 @@ int main()
5660
//counters
5761
int went_back_to_start_count = 0;
5862
int failure_count = 0;
63+
int max_steps_before_going_back = 0;
64+
vector<int> steps_it_failed;
5965
for(int i=0; i < NUMBER_OF_TRY; ++i){
6066

6167
int percent = (100 * i) / NUMBER_OF_TRY;
@@ -80,21 +86,25 @@ int main()
8086
int walk_percent = 100*( (float) j / MAX_TRY_BEFORE_GIVEUP);
8187
if (walk_percent >= walk_nextPrint)
8288
{
83-
//std::cout << "\rWalk: " << std::string(walk_percent/5, '|') << walk_percent << "%";
89+
std::cout << "\rWalk: " << std::string(walk_percent/5, '|') << walk_percent << "%";
8490
std::cout.flush();
8591
walk_nextPrint += walk_step;
8692
}
8793

8894
//go +1 or -1 ?
89-
int step = 0;
95+
int aStep = 0;
9096
if (std::rand() % 2 == 0){
91-
step = 1;
97+
aStep = 1;
9298
}else{
93-
step = -1;
99+
aStep = -1;
94100
}
95101

96102
//go +1 or -1 on a rdm direction
97-
current_point[distr_index(gen)]+=step;
103+
int random_index = distr_index(gen);
104+
current_point[random_index] += aStep;
105+
if ( MODULO > 0 && random_index == (DIMENSION-1)) {
106+
current_point[random_index] = (current_point[random_index] + MODULO ) % MODULO;
107+
}
98108

99109
//did we went back to zero ?
100110
bool went_back_to_start = true;
@@ -108,6 +118,10 @@ int main()
108118
if (went_back_to_start) {
109119
cout << " went_back_to_start loop " << i << " after " << j+1 << " steps " << endl;
110120
went_back_to_start_count++;
121+
steps_it_failed.push_back(j+1);
122+
if (j+1 > max_steps_before_going_back){
123+
max_steps_before_going_back = j+1;
124+
}
111125
break;
112126
}else if (j==MAX_TRY_BEFORE_GIVEUP-1){
113127
failure_count++;
@@ -121,6 +135,13 @@ int main()
121135
}
122136
}
123137

138+
std::sort (steps_it_failed.begin(), steps_it_failed.end());
124139
std::cout << "went_back_to_start_count " << went_back_to_start_count << endl;
125140
std::cout << "failure_count " << failure_count << endl;
141+
std::cout << "max_steps_before_going_back " << max_steps_before_going_back << endl;
142+
cout << "steps_it_failed = [ ";
143+
for (auto const &here: steps_it_failed) {
144+
cout << here << ", ";
145+
}
146+
cout << " ]" << endl;
126147
}

random_walk_vector.cpp

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <random>
5+
#include <limits>
6+
#include <algorithm>
7+
#include <iomanip>
8+
9+
using namespace std;
10+
11+
12+
13+
bool no_need_to_to_continue(vector<int> current_point, int index, int maxIndex) {
14+
int total = 0;
15+
for(int val : current_point){
16+
total+=std::abs(val);
17+
}
18+
if (total > (maxIndex-index) ){
19+
cout << " no_need_to_to_continue(" << maxIndex-index << ")" << " total=" << total << endl;
20+
return true;
21+
}
22+
return false;
23+
}
24+
25+
class Points {
26+
public :
27+
28+
Points() = default;
29+
30+
Points(int aDimension)
31+
{
32+
for(int i=0; i < aDimension; ++i){
33+
_current_point.push_back(0);
34+
}
35+
}
36+
37+
bool is_zero(){
38+
for(int k=0; k < _current_point.size(); ++k){
39+
if(_current_point[k]!=0){
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
46+
void print_point() {
47+
cout << "_current_point=" ;
48+
for(int l=0; l < _current_point.size(); ++l){
49+
cout << _current_point[l] << ", " ;
50+
}
51+
cout << endl;
52+
}
53+
54+
vector<int> _current_point;
55+
};
56+
57+
int main()
58+
{
59+
unsigned int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<unsigned int>::max();
60+
//int MAX_TRY_BEFORE_GIVEUP = 10000;
61+
int DIMENSION = 3;
62+
int NUMBER_OF_TRY = 1000;
63+
int MODULO = 3;
64+
cout << "MAX_TRY_BEFORE_GIVEUP=" << MAX_TRY_BEFORE_GIVEUP << endl;
65+
cout << "DIMENSION=" << DIMENSION << endl;
66+
cout << "NUMBER_OF_TRY=" << NUMBER_OF_TRY << endl;
67+
cout << "MODULO=" << MODULO << endl;
68+
69+
std::random_device rd; // obtain a random number from hardware
70+
std::mt19937 gen(rd()); // seed the generator
71+
std::uniform_int_distribution<> distr_index(0, DIMENSION-1); // define the range
72+
73+
74+
//fill the vector with the dimension size number of try
75+
76+
vector<Points> global_vector;
77+
for(int i=0; i < NUMBER_OF_TRY; ++i){
78+
global_vector.push_back(Points(DIMENSION));
79+
}
80+
81+
//to print overall progress
82+
double step = 0.1;
83+
double nextPrint = step;
84+
85+
//counters
86+
int went_back_to_start_count = 0;
87+
int failure_count = 0;
88+
int max_steps_before_going_back = 0;
89+
for(unsigned int i=0; i < MAX_TRY_BEFORE_GIVEUP; ++i){
90+
91+
double percent = 100*( (double) i / MAX_TRY_BEFORE_GIVEUP);
92+
if (percent >= nextPrint)
93+
{
94+
float failures = (100.0 * global_vector.size() ) / NUMBER_OF_TRY;
95+
std::cout << "FAILURES " << std::fixed << setprecision(2) << failures << "%"
96+
<< " Global progress: " << setfill(' ') << setw(2) << percent << "%" << " steps done: " << i << endl;
97+
std::cout.flush();
98+
if(nextPrint>2){
99+
nextPrint += 10*step;
100+
}else{
101+
nextPrint += step;
102+
}
103+
}
104+
//go one step for each point in the global vector
105+
for (auto it = global_vector.begin(); it != global_vector.end(); ){
106+
107+
//go +1 or -1 ?
108+
int aStep = 0;
109+
if (std::rand() % 2 == 0){
110+
aStep = 1;
111+
}else{
112+
aStep = -1;
113+
}
114+
115+
//go +1 or -1 on a rdm direction
116+
int random_index = distr_index(gen);
117+
it->_current_point[random_index] += aStep;
118+
if ( MODULO > 0 && random_index == (DIMENSION-1)) {
119+
it->_current_point[random_index] = (it->_current_point[random_index] + MODULO ) % MODULO;
120+
}
121+
122+
if(it->is_zero()){
123+
std::cout << "i = " << i << " going to erase ";
124+
it->print_point();
125+
global_vector.erase(it);
126+
went_back_to_start_count++;
127+
}else{
128+
++it;
129+
}
130+
}
131+
}
132+
133+
failure_count=global_vector.size();
134+
std::cout << "went_back_to_start_count " << went_back_to_start_count << endl;
135+
std::cout << "failure_count " << failure_count << endl;
136+
std::cout << "max_steps_before_going_back " << max_steps_before_going_back << endl;
137+
138+
}

0 commit comments

Comments
 (0)