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

4 years ago
4 years ago
  1. #include <stdint.h>
  2. #include "rand.h"
  3. typedef int32_t prng_gen;
  4. // The following line is stolen from a Verilog thing I wrote a while ago, which borrowed the binary digits from a paper I read
  5. // poly <= lfsrmode[0] ? 32'b10100011000000000000000000000000 : (lfsrmode[1] ? 16'b1011010000000000 : 8'b10011101);
  6. // The biary digits have here been translated to hex, because for some reason C doesn't support binary literals
  7. int lfsr_iterate(int32_t prev)
  8. {
  9. int32_t poly = 0xA3000000;
  10. int32_t mask = prev & poly;
  11. int32_t ii, accum = 0, cbit = 1;
  12. for (ii = 0; ii < 32; ii++)
  13. {
  14. accum ^= !(mask & cbit); // Since there are 32 bits, the ! should only normalize, not invert the result.
  15. cbit <<= 1;
  16. }
  17. prev <<= 1;
  18. prev |= accum;
  19. return prev;
  20. }
  21. int32_t lfsr_nextnum(int32_t prev)
  22. {
  23. int ii;
  24. for (ii = 0; ii < 33; ii++)
  25. {
  26. prev = lfsr_iterate(prev);
  27. }
  28. return prev;
  29. }
  30. int32_t prng()
  31. {
  32. return prn = lfsr_nextnum(prn);
  33. }
  34. double prngd() // Random double between 0 and 1
  35. {
  36. return (double) ((uint32_t) prng()) / (((uint64_t) 1 << 32) - 1);
  37. }
  38. double prngdn() // Normal random number generator - mean 0 stdev 1
  39. {
  40. const int n_iter_sqrt = 5; // Higher values for better approximations
  41. const int n_iter = n_iter_sqrt * n_iter_sqrt;
  42. double accum = 0.0;
  43. int ii;
  44. for (ii = 0; ii < n_iter; ii++)
  45. {
  46. accum += prngd();
  47. }
  48. return (accum - 0.5 * n_iter) * 0.70710678118;
  49. }
  50. double remap(double x, double lowin, double highin, double lowout, double highout) // Linear transform from one range onto another
  51. {
  52. return lowout + (highout - lowout) / (highin - lowin) * (x - lowin);
  53. }
  54. double uremap(double x, double lowout, double highout) // Linear transform from [0, 1] onto another range
  55. {
  56. return lowout + (highout - lowout) * x;
  57. }
  58. double mremap(double x, double meanin, double stdevin, double meanout, double stdevout) // Linear transform from one mean and stdev to another
  59. {
  60. return (x - meanin) * stdevout / stdevin + meanout;
  61. }
  62. double umremap(double x, double mean, double stdev) // Linear tranform from mean 0 stdev 1 to some other mean and stdev
  63. {
  64. return x * stdev + mean;
  65. }
  66. int bound_int(int x, int low, int high)
  67. {
  68. return x < low ? low : x > high ? high : x;
  69. }