Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
FastRandom is a small library made to generate random enough numbers and UUIDs. The randomity is not that guarranted, but at least, the output have really 100% of chance of being trash.

```c++
#include <FastRandom/UUID.h>
#include <FastRandom/uuid.h>

archivist::UUID weak{fast_uuid()}; // generate a hasty UUID
archivist::UUID medium{balanced_uuid()}; // generate a probably strong UUID
archivist::UUID good{strong_uuid()}; // generate a really random UUID
archivist::uuid_fast weak; // generate a hasty UUID
archivist::uuid_balanced medium; // generate a probably strong UUID
archivist::uuid_strong good; // generate a really random UUID
```

```c++
#include <FastRandom/Base_PRNG.h>
#include <FastRandom/base_prng.h>

uint64_t number1 = archivist::PRNG(); // Generate a random number
uint64_t number2 = archivist::PRNG(4682); // Generate a random number and mixes some entropy (thread wide)
archivist::PRNG_feed(some_entropy); // Give some entropy system wide
uint64_t number1 = archivist::prng(); // Generate a random number
uint64_t number2 = archivist::prng(4682); // Generate a random number and mixes some entropy (thread wide)
archivist::prng_feed(some_entropy); // Give some entropy system wide
```
39 changes: 0 additions & 39 deletions include/FastRandom/Base_PRNG.h

This file was deleted.

62 changes: 0 additions & 62 deletions include/FastRandom/UUID.h

This file was deleted.

42 changes: 42 additions & 0 deletions include/FastRandom/base_prng.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licenced under MIT Licence
*
* (c) Ludovic 'Archivist' Lagouardette 2018
*
*/

#pragma once
#include <mutex>
#include <stdint.h>
#include <atomic>

namespace archivist
{

namespace internal
{
extern thread_local uint64_t prngState;
extern thread_local uint64_t prngState_c;
extern thread_local uint64_t prngState_a;
extern std::atomic<uint64_t> _entropy_contributor;
}

inline uint64_t prng(uint64_t gift = 42)
{
internal::prngState_c += internal::_entropy_contributor.load(std::memory_order_relaxed);
internal::prngState_a = (internal::prngState_a << 17) + internal::prngState_c;
internal::prngState ^= gift;
internal::prngState = internal::prngState * internal::prngState_a + internal::prngState_c;
return internal::prngState;
}

inline void prng_feed(uint64_t feed)
{
internal::_entropy_contributor.fetch_xor(feed, std::memory_order_relaxed);
}

inline void prng_feed_alt(uint64_t feed)
{
internal::_entropy_contributor.fetch_xor(feed, std::memory_order_relaxed);
}
}
92 changes: 92 additions & 0 deletions include/FastRandom/uuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#ifndef archivist_uuid_h
#define archivist_uuid_h

/**
* Licenced under MIT Licence
*
* (c) Ludovic 'Archivist' Lagouardette 2018
*
*/

#include "FastRandom/base_prng.h"
#include <chrono>
#include <iostream>
#include <string>
#include <random>

namespace archivist
{

enum class uuid_modes
{
balanced, fast, strong
};

struct basic_uuid
{
static thread_local std::random_device src;
static thread_local uint8_t balance;
uint64_t state[2]{};

inline void init_default()
{
state[0] = prng(9998);
state[1] = prng(17);
}

inline void init_strong()
{
uint32_t internal = src();
uint32_t cross = src() | std::chrono::duration_cast< std::chrono::microseconds >(
std::chrono::system_clock::now().time_since_epoch()).count();

state[0] = prng(internal);
prng_feed(cross);
state[1] = prng(cross);
}

bool operator==(const basic_uuid& rhs) const
{
return (state[0] == rhs.state[0]) && (state[1] == rhs.state[1]);
}

bool operator<(const basic_uuid& rhs) const
{
return (state[0] < rhs.state[0]) || ((state[1] < rhs.state[1]) && (state[0] == rhs.state[0]));
}
};

using uuid = archivist::basic_uuid;

struct uuid_balanced : public basic_uuid
{
uuid_balanced()
{
balance++;
if(balance % 128)
init_default();

else
init_strong();
}
};

struct uuid_fast : public basic_uuid
{
uuid_fast()
{
init_default();
}
};

struct uuid_strong : public basic_uuid
{
uuid_strong()
{
init_strong();
}
};

}

#endif//archivist_uuid_h
26 changes: 0 additions & 26 deletions src/FastRandom/Base_PRNG.cpp

This file was deleted.

Loading