mercredi 22 juillet 2015

Generating random float number in C++ doesn't work (Visual studio 2013)

I know there are many duplicates of this question around but my problem is different. What I am trying to do is generating a simple random float number in the range [-0.5, 0.5]. What I have been using is the formula expressed in this link:

C++ random float number generation

I need two different random float numbers jitter_dx and jitter_dy. According to the formula of the link above I have:

float jitter_dx = -0.5f + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (0.5f - (-0.5f))));
float jitter_dy = -0.5f + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (0.5f - (-0.5f))));

Now, my problem is weird because when I try to debug the code, jitter_dx seems being always the same (or slightly different) while jitter_dy is generated quite randomly. I hereby give you the values of both jitter_dx and jitter_dy during 10 different debugging sessions:

0.213156521, 0.123004854

0.215659022, 0.323389411

0.216849267, 0.259575188

0.217856407, -0.460295409

0.218955040, 0.147907972

0.220358908, -0.259910882

0.221335471, 0.0202490091

0.224143207, 0.204702914

0.225333393, 0.140888691

As you can see jitter_dx is always (almost) the same. Both values are calculated one right after the other one, without any code in the middle. May you help me finding the problem? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire