The C language has a non-cryptographic PRNG with the srand(), rand() interface. The standard also contains a portable example implementation,
for the smallest allowed value of RAND_MAX:
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
unfortunately, the 32-bit multiplication seems a bit too expensive for low-end embedded systems.
I'm looking for a good, cheap alternative PRNG. It should only use cheap operations, such as bitwise opeations, additions or subtractions.
Any recommendations or pointers to good publications or books on the topic?
Philipp
Why not rc4? It is a cryptographic strength random number generator.
Well, there is some evidence that at the 1 part in about 10^6 the distribution is not precisely flat. It uses only byte swaps
and additions.
The C language has a non-cryptographic PRNG with the srand(), rand() interface. The standard also contains a portable example implementation,
for the smallest allowed value of RAND_MAX:
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
unfortunately, the 32-bit multiplication seems a bit too expensive for low-end embedded systems.
I'm looking for a good, cheap alternative PRNG. It should only use cheap operations, such as bitwise opeations, additions or subtractions.
Any recommendations or pointers to good publications or books on the
topic?
Philipp
On Wed, 23 Sep 2015 06:48:06 +1000, Philipp Klaus Krause <pkk@spth.de>
wrote:
The C language has a non-cryptographic PRNG with the srand(), rand()
interface. The standard also contains a portable example implementation,
for the smallest allowed value of RAND_MAX:
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
unfortunately, the 32-bit multiplication seems a bit too expensive for
low-end embedded systems.
I'm looking for a good, cheap alternative PRNG. It should only use cheap
operations, such as bitwise opeations, additions or subtractions.
Any recommendations or pointers to good publications or books on the
topic?
Philipp
You could rewrite the rand function you gave above to use int's and
change the parameters to
next = next * 17 + 5
the trick here being that this is equivalent to:
next = next < 4 + next + 5
the '< 4' being a left shift 4 places, which is equivalent to multiply
by 16.
However LCG random number generators are often not great with micro-controllers. The bits change in a regular order
On 23.09.2015 00:35, William Unruh wrote:
Why not rc4? It is a cryptographic strength random number generator.
Well, there is some evidence that at the 1 part in about 10^6 the
distribution is not precisely flat. It uses only byte swaps
and additions.
I can't afford the byte-array used in rc4. The devices I want to target typically have 128B to 32KB of RAM total.
I'd prefer not to have to keep storage for more than 4 bytes in between obtaining two subsequent pseudo-random numbers.
Philipp
You could try XORShift
https://en.wikipedia.org/wiki/Xorshift
The C language has a non-cryptographic PRNG with the srand(), rand() interface. The standard also contains a portable example implementation,
for the smallest allowed value of RAND_MAX:
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
unfortunately, the 32-bit multiplication seems a bit too expensive for low-end embedded systems.
I'm looking for a good, cheap alternative PRNG. It should only use cheap operations, such as bitwise opeations, additions or subtractions.
Any recommendations or pointers to good publications or books on the
topic?
Philipp
On 23.09.2015 02:08, David Eather wrote:
You could try XORShift
https://en.wikipedia.org/wiki/Xorshift
I had a look at Marsaglia's paper "Xorshift RNGs". The 32-bit one could
be cheap enough; he recommends parameters (13, 17, 5). Are those
parameters a good choice?
I'd consider use the lower 16-bit as the PRNG output.
Is that a reasonable choice? How does it compare in quality to using the lower 16-bit of a 32-bit or 24-bit Galois-LFSR?
On 23.09.2015 02:08, David Eather wrote:
You could try XORShift
https://en.wikipedia.org/wiki/Xorshift
I had a look at Marsaglia's paper "Xorshift RNGs". The 32-bit one could
be cheap enough; he recommends parameters (13, 17, 5). Are those
parameters a good choice?
I'd consider use the lower 16-bit as the PRNG output.
Is that a reasonable choice? How does it compare in quality to using the lower 16-bit of a 32-bit or 24-bit Galois-LFSR?
Philipp
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 72:13:29 |
Calls: | 6,657 |
Calls today: | 3 |
Files: | 12,203 |
Messages: | 5,332,304 |
Posted today: | 1 |