Generating a random double between a range of values

后端 未结 5 1372
天涯浪人
天涯浪人 2021-01-13 18:33

Im currently having trouble generating random numbers between -32.768 and 32.768. It keeps giving me the same values but with a small change in the decimal field. ex : 27.xx

5条回答
  •  梦谈多话
    2021-01-13 18:43

    It seams to be plainly obvious but some of the examples say otherwise... but i thought when you divide 1 int with another you always get an int? and you need to type cast each int to double/float before you divide them.

    ie: double r = (68.556* (double)rand()/(double)RAND_MAX - 32.768);

    also if you call srand() every time you call rand() you reset the seed which results in similar values returned every time instead of ''random'' ones.

提交回复
热议问题