Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions practice/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <immintrin.h>
#include <vector>
#include <sys/random.h>
#include <ctime>

using namespace std;

int get_random_std()
{
return rand() % 200;
}

int get_random_rdrand()
{
unsigned int ret;
int rc = _rdrand32_step(&ret);
if (rc == 1) return ret;
return -1;
}

int get_random_dev_urandom()
{
int ret, read;
FILE *urandom = fopen("/dev/urandom", "r");
read = fread(&ret, sizeof(ret), 1, urandom);
fclose(urandom);
if (read > 0) return ret;
return -1;
}

int get_random_getrandom()
{
int ret, read;
read = getrandom(&ret, sizeof(ret), 0);
if (read > 0) return ret;
return read;
}

int get_random(int (*random_func)())
{
return random_func() % 200;
}

typedef int (*fp)();

int main()
{
int n;
cout << "Please enter a number" << endl;
cin >> n;

srand((unsigned) time(NULL));

vector<fp> randoms = {
get_random_getrandom,
get_random_dev_urandom,
get_random_rdrand,
get_random_std
};

for (int i = 0; i < 5; i++)
{
if (get_random(randoms[i % 4]) == 150)
n += 1;
else
i--;
}

cout << "Your number + 5 is: " << n << endl;
return 0;
}