Random number help

Thomas Nyberg tomuxiong at gmx.com
Wed Nov 23 14:33:54 EST 2016


On 11/23/2016 02:17 PM, Thomas Grops via Python-list wrote:
> I need a way of generating a random number but there is a catch:
>
> I don't want to include certain numbers, is this possible?
>
> random.randint(1,100) works as it will randomly pick numbers between 1 and 100 but say i don't want 48 to come out is there a way of doing this. It needs to be an integer too so not a list unless there is a way to convert list to int
>
> Many Thanks Tom
>
If you specifically want to exclude 48, you could create the list of 
acceptable numbers and make a random choice from it. For example:

 >>> import random
 >>> l = list(range(1, 48)) + list(range(49, 101))
 >>> random.choice(l)
64

Cheers,
Thomas



More information about the Python-list mailing list