0

I'm having a problem with my code, in which I try to generate a number between 0 and a dim user-defined variable. The line I'm having problem is:

arrayPos = rand()%dim;

I already called srand(time(NULL)) and the arrayPos is getting some wierd numbers like 9.267e-315 and so on.
Any ideas on how to fix it?

Thank you

Pedro
  • 333
  • 1
  • 5
  • 24
  • I'm pretty certain this has been asked on Stackoverflow before... – us2012 Sep 12 '13 at 19:06
  • If you're in C++ and especially C++11 you should avoid `rand()` it should be considered [harmful](http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) furthermore using `% dim` does not produce a uniform distribution and should be avoided (the video I linked to explains why) – Mgetz Sep 12 '13 at 19:09
  • Your code should work (somewhat reasonably) as written. 9.267e-315 looks like "random garbage in an uninitialized variable" to me. The problem likely lies elsewhere, in the code you haven't shown. – Igor Tandetnik Sep 12 '13 at 19:11
  • 1
    How is arrayPos defined? How is dim defined? How did you print things out? etc etc etc – Charlie Burns Sep 12 '13 at 19:12

1 Answers1

0

The problem really was garbage memory, I restarted the computer and it worked like a charm

Pedro
  • 333
  • 1
  • 5
  • 24