Generating a random double between a range of values

后端 未结 5 1322
天涯浪人
天涯浪人 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:56

    So, I think this is a typical case of "using time(NULL) isn't a great way of seeding random numbers for runs that start close together". There isn't that many bits that change in time(NULL) from one call to the next, so random numbers are fairly similar. This is not a new phenomena - if you google "my random numbers aren't very random", you'll find LOTS of this.

    There are a few different solutions - getting a microsecond or nanosecond time would be the simplest choice - in Linux gettimeofday will give you a microsecond time as part of the struct.

提交回复
热议问题