Program Listing for File Utils.hpp

Return to documentation for file (gui/Utils/Utils.hpp)

#include <random>
#include <string>

constexpr unsigned int hash(const char *str)
{
  unsigned int h = 5381;  // Initial hash value
  for (; *str != '\0'; str++)
    h = (h * 33) ^ (unsigned char)*str;
  return h;
}

constexpr unsigned int hash(std::string &str)
{
  const char *tmp_str = str.c_str();

  unsigned int h = 5381;  // Initial hash value
  for (; *tmp_str != '\0'; tmp_str++)
    h = (h * 33) ^ (unsigned char)*tmp_str;
  return h;
}

inline float randomFloat(float min, float max)
{
  static std::random_device rd;
  static std::mt19937 gen(rd());
  std::uniform_real_distribution<float> dis(min, max);

  return dis(gen);
}

inline double hashToRange(double x, double btw = 1.0)
{
  double hashed = std::sin(x * 12.9898) * 43758.5453;
  return (hashed - std::floor(hashed)) - btw;
}