Project 2 for MA132
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
2.2 KiB

#include <stdint.h>
#include "rand.h"
typedef int32_t prng_gen;
// The following line is stolen from a Verilog thing I wrote a while ago, which borrowed the binary digits from a paper I read
// poly <= lfsrmode[0] ? 32'b10100011000000000000000000000000 : (lfsrmode[1] ? 16'b1011010000000000 : 8'b10011101);
// The biary digits have here been translated to hex, because for some reason C doesn't support binary literals
int lfsr_iterate(int32_t prev)
{
int32_t poly = 0xA3000000;
int32_t mask = prev & poly;
int32_t ii, accum = 0, cbit = 1;
for (ii = 0; ii < 32; ii++)
{
accum ^= !(mask & cbit); // Since there are 32 bits, the ! should only normalize, not invert the result.
cbit <<= 1;
}
prev <<= 1;
prev |= accum;
return prev;
}
int32_t lfsr_nextnum(int32_t prev)
{
int ii;
for (ii = 0; ii < 33; ii++)
{
prev = lfsr_iterate(prev);
}
return prev;
}
int32_t prng()
{
return prn = lfsr_nextnum(prn);
}
double prngd() // Random double between 0 and 1
{
return (double) ((uint32_t) prng()) / (((uint64_t) 1 << 32) - 1);
}
double prngdn() // Normal random number generator - mean 0 stdev 1
{
const int n_iter_sqrt = 5; // Higher values for better approximations
const int n_iter = n_iter_sqrt * n_iter_sqrt;
double accum = 0.0;
int ii;
for (ii = 0; ii < n_iter; ii++)
{
accum += prngd();
}
return (accum - 0.5 * n_iter) * 0.70710678118;
}
double remap(double x, double lowin, double highin, double lowout, double highout) // Linear transform from one range onto another
{
return lowout + (highout - lowout) / (highin - lowin) * (x - lowin);
}
double uremap(double x, double lowout, double highout) // Linear transform from [0, 1] onto another range
{
return lowout + (highout - lowout) * x;
}
double mremap(double x, double meanin, double stdevin, double meanout, double stdevout) // Linear transform from one mean and stdev to another
{
return (x - meanin) * stdevout / stdevin + meanout;
}
double umremap(double x, double mean, double stdev) // Linear tranform from mean 0 stdev 1 to some other mean and stdev
{
return x * stdev + mean;
}
int bound_int(int x, int low, int high)
{
return x < low ? low : x > high ? high : x;
}