Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Pharap
Last active January 29, 2018 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pharap/dff24b37d0abcf2cd0195325211f0af6 to your computer and use it in GitHub Desktop.
Save Pharap/dff24b37d0abcf2cd0195325211f0af6 to your computer and use it in GitHub Desktop.
Arduboy specific random number utility example
/*
Copyright (C) 2018 Pharap (@Pharap)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <Arduboy2.h>
template< typename Object >
class ResultDeducer
{
public:
Object * object;
ResultDeducer(void) = delete;
using Type = decltype((*object)());
};
template< typename Random >
using randomResult = typename ResultDeducer<Random>::Type;
// Modified version of OpenBSD's arc4random_uniform
template< typename Random >
randomResult<Random> uniformRandom(const Random & random, randomResult<Random> max)
{
using UInt = randomResult<Random>;
const UInt min = -max % max;
UInt result = 0;
while(true)
{
result = random();
if(result >= min)
return result % max;
}
}
template< typename UInt, typename Random >
UInt uniformRandom(Random random, UInt min, UInt max)
{
return min + uniformRandom(random, (max - min));
}
class MarsagliaShr3
{
public:
using SeedType = uint32_t;
using ResultType = uint32_t;
private:
uint32_t seed = 1;
uint32_t state = 1;
uint32_t next(uint32_t value)
{
value ^= value * (static_cast<uint32_t>(1) << 17);
value ^= value >> 13;
value ^= value * (static_cast<uint32_t>(1) << 5);
return value;
}
public:
MarsagliaShr3(void) = default;
MarsagliaShr3(uint32_t seed)
: seed(seed), state(seed)
{
}
uint32_t getSeed(void) const
{
return this->seed;
}
void reseed(uint32_t seed)
{
this->seed = seed;
this->state = state;
}
uint32_t next(void)
{
this->state = this->next(this->state);
return this->state;
}
// For convenience
uint32_t operator() (void)
{
return this->next();
}
};
class PinEntropy32
{
public:
static uint32_t generateEntropy(void)
{
power_adc_enable();
ADCSRA |= (1 << ADSC);
while(bit_is_set(ADCSRA, ADSC));
const uint32_t result = (static_cast<uint32_t>(ADC) << 16) + micros();
power_adc_disable();
return result;
}
};
// Something like this?
class PinEntropy16
{
public:
static uint16_t generateEntropy(void)
{
power_adc_enable();
ADCSRA |= (1 << ADSC);
while(bit_is_set(ADCSRA, ADSC));
const uint16_t result = static_cast<uint16_t>(ADC + micros());
power_adc_disable();
return result;
}
};
template< typename UInt >
class PinEntropy;
template<> class PinEntropy<uint32_t> : public PinEntropy32 {};
template<> class PinEntropy<uint16_t> : public PinEntropy16 {};
template< typename RandomEngine, template<typename> class EntropySource = PinEntropy >
class EntropyReseeder
{
public:
using SeedType = typename RandomEngine::SeedType;
using ResultType = typename RandomEngine::ResultType;
using EntropySourceType = EntropySource<SeedType>;
private:
RandomEngine engine;
public:
EntropyReseeder(void) = default;
EntropyReseeder(const RandomEngine & engine)
: engine(engine)
{
}
EntropyReseeder(RandomEngine && engine)
: engine(engine)
{
}
void reseed(SeedType seed)
{
this->engine.reseed(seed);
}
ResultType next(void)
{
const auto result = this->engine();
if(result == this->engine.getSeed())
{
this->engine.reseed(EntropySourceType::generateEntropy());
}
return result;
}
// For convenience
ResultType operator() (void)
{
return this->next();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment