How do you generate a random double uniformly distributed between 0 and 1 from C++?

后端 未结 13 613
暗喜
暗喜 2020-11-28 22:28

How do you generate a random double uniformly distributed between 0 and 1 from C++?

Of course I can think of some answers, but I\'d like to know what the standard pr

13条回答
  •  半阙折子戏
    2020-11-28 23:29

    An old school solution like:

    double X=((double)rand()/(double)RAND_MAX);
    

    Should meet all your criteria (portable, standard and fast). obviously the random number generated has to be seeded the standard procedure is something like:

    srand((unsigned)time(NULL));
    

提交回复
热议问题