0

I need a random list of integers without 0. I'm using random.sample(xrange(y),z) but I don't want 0 in this list. Thank you

user3250719
  • 105
  • 1
  • 2
  • 7

2 Answers2

2

Start your range at 1 then:

random.sample(xrange(1, y), z)

That's all there is to it, really.

Demo:

>>> list(xrange(3))
[0, 1, 2]
>>> list(xrange(1, 3))
[1, 2]

xrange() doesn't just produce a series of integers up to an endpoint, it can also produce a series between two points; a third option gives you a step size:

>>> list(xrange(1, 6, 2))
[1, 3, 5]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @Martijin thank you very much. And if I want that xrange, always without 0, is random in 1,100 but the maximum value multiplyed a number like 0,7 is <= the minimum value? – user3250719 Apr 03 '14 at 19:28
  • I'm not sure what you are asking; if you want to limit the range, then just pass calculated values to the `xrange()` calls. – Martijn Pieters Apr 03 '14 at 20:54
2

Simple, just specify another argument:

random.sample(xrange(1, y), z)
                     ^

Notice the 1. This is the start argument, meaning that 0 is not included here.

Example:

>>> list(xrange(1, 10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
anon582847382
  • 19,907
  • 5
  • 54
  • 57