Why does C++ rand() seem to generate only numbers of the same order of magnitude?

前端 未结 9 2063
孤独总比滥情好
孤独总比滥情好 2021-01-30 01:14

In a small application written in C/C++, I am facing a problem with the rand function and maybe the seed :

I want to produce a sequence of random numbers th

相关标签:
9条回答
  • 2021-01-30 01:38

    use pow(2,rand()) it will give the answers in order of desired magnitude!!

    0 讨论(0)
  • 2021-01-30 01:40

    You need to be more precise: you want different base 2 logarithm values but what distribution do you want for this? The standard rand() functions generate a uniform distribution, you will need to transform this output using the quantile function associated with the distribution that you want.

    If you tell us the distribution then we can tell you the quantile function you need.

    0 讨论(0)
  • 2021-01-30 01:42

    If you want different orders of magnitude, why not simply try pow(2, rand())? Or perhaps choose the order directly as rand(), as Harold suggested?

    0 讨论(0)
  • 2021-01-30 01:44

    There are only 3% of numbers between 1 and 230 which are NOT between 225 and 230. So, this sounds pretty normal :)

    Because 225 / 230 = 2-5 = 1/32 = 0.03125 = 3.125%

    0 讨论(0)
  • 2021-01-30 01:47

    The lighter green is the region between 0 and 225; the darker green is the region between 225 and 230. The ticks are powers of 2.

    distribution

    0 讨论(0)
  • 2021-01-30 01:48

    If you want to use random numbers from an online service you can use wget for that, you may want to see you can also use services like random.org for your random number generation , you can catch them using wget and then reading the numbers from the downloaded file

    wget -q https://www.random.org/integers/?num=100&min=1&max=100&col=5&base=10&format=html&rnd=new -O new.txt
    

    http://programmingconsole.blogspot.in/2013/11/a-better-and-different-way-to-generate.html

    0 讨论(0)
提交回复
热议问题