Talk:Pick random element: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "The task should clarify if the elements should be taken at random with equal probability (that is, discrete uniform distribution, insofar as the underlying pseudorandom genera...")
 
No edit summary
Line 1: Line 1:
The task should clarify if the elements should be taken at random with equal probability (that is, discrete uniform distribution, insofar as the underlying pseudorandom generator allows this). It seems obvious, but it's not stated, and if so, there are wrong entries in the page.
The task should clarify if the elements should be taken at random with equal probability (that is, discrete uniform distribution, insofar as the underlying pseudorandom generator allows this). It seems obvious, but it's not stated, and if so, there are wrong entries in the page.


For instance, the C program does not yield a uniform distribution. The correct solution is explained in the accepted answer to this Stack Overflow question: '''[https://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range How to generate a random number from within a range]'''.
For instance, the C program does not yield a uniform distribution. A correct solution is explained in the accepted answer to this Stack Overflow question: '''[https://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range How to generate a random number from within a range]'''. It describes a form of '''[https://en.wikipedia.org/wiki/Rejection_sampling rejection sampling]''', and one would expect a similar approach in a correct implementation. For instance, Python does this for the '''[https://github.com/python/cpython/blob/master/Lib/random.py random.randint]''' function.


The same bad C code is reused in the [[Loops/Break]] task, and I fear many could use the Rosetta Code program as is without suspecting a problem.
The same bad C code is reused in the [[Loops/Break]] task, and I fear many could use the Rosetta Code program as is without suspecting a problem.

Revision as of 22:37, 26 August 2017

The task should clarify if the elements should be taken at random with equal probability (that is, discrete uniform distribution, insofar as the underlying pseudorandom generator allows this). It seems obvious, but it's not stated, and if so, there are wrong entries in the page.

For instance, the C program does not yield a uniform distribution. A correct solution is explained in the accepted answer to this Stack Overflow question: How to generate a random number from within a range. It describes a form of rejection sampling, and one would expect a similar approach in a correct implementation. For instance, Python does this for the random.randint function.

The same bad C code is reused in the Loops/Break task, and I fear many could use the Rosetta Code program as is without suspecting a problem.

Eoraptor (talk) 22:26, 26 August 2017 (UTC)