-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorran.cpp
68 lines (54 loc) · 1.26 KB
/
norran.cpp
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
/*
* File: norran.cpp
* About File: Algoritma Non-Repetitive Random Number Generator (NORRAN)
* Author: Yoga Budi Yulianto
*
* Created on May 22, 2018, 5:42 PM
* Updated on May 17, 2022
*/
#include <iostream>
using namespace std;
void generateRandomNumber(int hasil[], int panjangArray) {
srand(time(NULL));
hasil[0] = (rand() % panjangArray);
for(int i = 1; i < panjangArray; i++) {
hasil[i] = -1;
}
for(int i = 1; i < panjangArray; i++) {
bool valid = true;
int r = 0;
int angkaRandom = (rand() % panjangArray);
while(hasil[r] != -1) {
if(hasil[r] == angkaRandom) {
valid = false;
break;
}
r++;
}
if(valid) {
hasil[i] = angkaRandom;
}
else {
i--;
}
}
}
int main(int argc, char** argv) {
int banyaknya = 10;
if(argc > 1) {
banyaknya = stoi(argv[1]);
}
int angkaRandom[banyaknya];
int panjangArray = sizeof(angkaRandom) / sizeof(int);
cout << "Algoritma NORRAN\n";
cout << "-----------------\n";
cout << "Jumlah Angka : " << panjangArray << "\n";
generateRandomNumber(angkaRandom, panjangArray);
cout << "Hasil : ";
for(int i = 0; i < panjangArray; i++) {
cout << angkaRandom[i] + 1 << " ";
}
cout << endl;
getchar();
return 0;
}