OpenGTA/math/weighted_set.cpp

38 lines
785 B
C++
Raw Normal View History

2015-12-03 00:37:37 +00:00
#include "weighted_set.h"
2018-09-23 06:35:20 +00:00
#include <cassert>
#include <ctime>
2015-12-03 00:37:37 +00:00
namespace Math {
WeightedSet::WeightedSet(unsigned int seed) :
2018-09-23 06:35:20 +00:00
elements()
{
std::srand(unsigned(std::time(0)));
2015-12-03 00:37:37 +00:00
}
void WeightedSet::add(unsigned int w, unsigned int e) {
while (w > 0) {
elements.push_back(e);
--w;
}
}
unsigned int WeightedSet::getRandom() {
//unsigned int rnd = (int) (totalWeight * (rand() / (RAND_MAX + 1.0)));
2018-09-23 06:35:20 +00:00
unsigned int rnd = std::rand() % (elements.size());
2015-12-03 00:37:37 +00:00
return elements[rnd];
}
}
/*
#include <iostream>
int main(int argc, char* argv[]) {
Util::Math::WeightedSet set(int(getpid()) ^ int(time(0)));
set.add(1, 1);
set.add(2, 2);
set.add(2, 3);
set.add(3, 4);
set.add(3, 5);
std::cout << set.getRandom() << std::endl;
}*/