[Tutor] ** Newbie ** - Fastest random choice from integers 0 to X?
Remco Gerlich
scarblac@pino.selwerd.nl
Thu, 7 Feb 2002 20:24:51 +0100
On 0, Chris McCormick <cmccormick@thestate.com> wrote:
> Hello all,
> I'm sure this is a simple answer, but I wasn't sure what would be fastest.
>
> I have a group of consecutive integers, 0 to X. Given only the top bound
> (X), what is the fastest way to produce a random integer, 0 and X inclusive?
> My guess is:
>
> random.randrange( 0, (X+1), 1 )
>
> Is this right?
Yes.
Apart from the speed, it's about the only right way, since this is a
standard library function for it, and the other one, random.randint, is
deprecated (shouldn't be used anymore).
Speed shouldn't be much of a concern, usually.
--
Remco Gerlich