C++ generating random numbers

后端 未结 11 1591
我寻月下人不归
我寻月下人不归 2020-12-01 10:49

My output is 20 random 1\'s, not between 10 and 1, can anyone explain why this is happening?

#include  
#include  
#include <         


        
11条回答
  •  自闭症患者
    2020-12-01 11:21

    Visual studio 2008 has no trouble with that program at all and happily generates a swathe of random numbers.

    What I would be careful of is the /(RAND_MAX +1.0) as this will likely fall foul of integer problems and end up with a big fat zero.

    Cast to double before dividing and then cast back to int afterwards

提交回复
热议问题