Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 24 Apr 2010
Offline Last Active Private

Posts I've Made

In Topic: Seeding the random number generator in <cstdlib>

Yesterday, 07:12 PM

Example of C++ <random>

#include <random>

int main() {
  //create a mersenne twister and seed it with a value from 'random device'
  //mersenne twister is good quality and decent speed. random device is low performance,
  //but delivers the best randomness the system can provide and doesn't need a seed
  //this creates the mt object, which is able to generate unshaped random values
  std::mt19937 rng(std::random_device{}());

  //but we're usually looking for numbers within some specific range, and that's easy to
  //screw up, so instead of just using the random bits indiscriminately we use a
  //distribution object to get exactly what we're after. For example, ints between 0 and 10
  std::uniform_int_distribution<int> zero_to_ten_dist(0,10);

  //then to get an int from it we just say:
  int my_num = zero_to_ten_dist(rng);

  //or if you want a float between zero and one instead
  std::uniform_real_distribution<float> normalized_float_dist(0.0f, 1.0f); //note that distribution ranges are inclusive
  //used the same way
  float my_float = normalized_float_dist(rng);

  //or what if we want true/false with 60% probability of true?
  std::bernoulli_distribution sixty_percent_dist(0.6);

  //used the same way
  bool my_bool = sixty_percent_dist(rng);

Of course some of the names used are confusing, but you don't actually need to memorize those. Once you include <random> your intellisense (or whatever you're using) should be able to find the mersenne twister just from "std::mt", and you'll usually want uniform int or real distributions. For the other stuff you can just look at the list of distributions on cppreference or cplusplus and see which one does what you're after. You can (and should) use the same generator object on a per-thread basis unless you have a good reason not to. The distributions are cheap to construct or move around, but the generators are not.


Someone will probably criticize the mersenne twister in here very soon, but in all honesty it can crap out a huge amount of quality randomness in a very short period of time, so I'd recommend putting some science on it (actually measure it) before getting complicated about things. That said, there are a number of other generators in the library, and they're described in the websites mentioned, so it's your call.


here's a link to the video I always link to

In Topic: The latest BMP I made crashes the program, other BMPs work fine

27 June 2016 - 01:21 PM


In Topic: The latest BMP I made crashes the program, other BMPs work fine

26 June 2016 - 01:57 PM

Is it better to use a PNG?


Yes, but that's not what's causing your error.

In Topic: [Solved] Rotate towards a target angle

25 June 2016 - 12:15 AM

DTT = direction to target (2D vector)
CH = current heading (2D vector)
CP = cross product of DTT and CH


CP = (DTT.x * CH.y) - (DTT.y * CH.x)

if(CP < 0) {
  //turn left
else if(CP > 0) {
  //turn right
else {
  //you're either pointing straight toward or straight away
  //you can compare signs to find out. if you're pointing towards
  //then the signs of both x's will be the same and the signs of both y's will be the same
  //if not then turn either direction (preferably in the direction toward the target's motion)
I don't know the synatx for bit math in Java, but on IEEE floats and doubles you can do a sign equality check with:

if( ((A ^ B) & SIGN_BIT_MASK) != 0) { signs are opposite }

In Topic: algorithmicly connecting a graph

22 June 2016 - 08:35 PM

Only create edges in the ne,e,se,s directions as you iterate?


Though really I'd have to agree that one-way edges is probably better for usage, since you generally want to know what outbound edges are available for a specific node. If your edges are of uniform weight you can even just represent them as a vector of destination node pointers within each node.