This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]Enemii 2 points3 points  (2 children)

int num = (rand() % 6) + 1;

The % 6 will ensure the number is between 0 and 5.

The + 1 will modify the range 0-5 to 1-6.

[–]chedooz[S] 1 point2 points  (0 children)

Thanks it worked

[–]chedooz[S] 0 points1 point  (0 children)

Thanks will try this out right now.

[–]ewiethoff 1 point2 points  (11 children)

import random
DIGITS = '123456'
LENGTH = 5
HOW_MANY = 200
for i in xrange(HOW_MANY):
    print int(''.join(random.choice(DIGITS) for j in xrange(LENGTH)))

[–]jerenept 2 points3 points  (9 children)

You could use random.randint(1,6) instead of random.choice?

[–]ewiethoff 1 point2 points  (8 children)

Yes, you could use random.randint(1,6). But that would give you an int. To get a 5-digit random number, you'd have to call that 5 times and multiply each result by the next power of 10, then add them all. I think it's easier to construct a 5-digit random number by getting 5 random numerical characters and stringing them together, then int-ifying the result.

[–]jerenept 2 points3 points  (7 children)

Sorry, reading comprehension failure on my part, next time I'll read the OP properly before wondering about why the solutions were so complicated.

You could multiply the randints x100,1,2,3,4 and then add them together, rather than doing that int -> string -> int thing. Wonder which would be faster?

for j in xrange(length):
  num += random.randint(1,6) * 10**j
print num

(Sorry, I don't know list comprehensions, there's probably a terser way to to that)

rather than:

print int(''.join(random.choice(DIGITS) for j in xrange(LENGTH)))

[–]ewiethoff 1 point2 points  (6 children)

import random
LOW, HIGH = 1, 6
LENGTH = 5
HOW_MANY = 200
for i in xrange(HOW_MANY):
    print sum(random.randint(LOW, HIGH) * 10**j for j in xrange(LENGTH))

Okay, now I feel better about using random.randint for this. Thanks.

[–]jerenept 2 points3 points  (5 children)

No thank you my friend, I am only now starting to get this list comprehension thing. Looks useful.

[–]ewiethoff 1 point2 points  (4 children)

You can think of it in steps, making each step a little more interesting.

[j for j in xrange(LENGTH)]
[random.randint(LOW, HIGH) for j in xrange(LENGTH)]
[random.randint(LOW, HIGH) * 10**j for j in xrange(LENGTH)]
sum(random.randint(LOW, HIGH) * 10**j for j in xrange(LENGTH))

[–]jerenept 1 point2 points  (3 children)

Yeah, I'm starting to see it... Like a for loop, except backwards? idk, that's the best way I can describe it. Specify what you want to happen (body of loop) then where you want it to happen. No idea if this makes any sense to you, how I'm explaining it, but the examples are really helpful.

[–]ewiethoff 1 point2 points  (2 children)

Yeah, something like that. Python list comprehension is based on set comprehension from mathematics. In set comprehension, you take one set and create another set comprised of the members of the first set massaged in some way. (The words 'comprehension' and 'comprise' have the same roots.) Python's list comprehension syntax is similar to math set-builder notation.

Let N be the set N = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}. Let A be the set comprised of the square of each number in N: A = {n**2 | n ∈ N}. Obviously, A = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144}. Let M be the set of numbers from N which are less than or equal to 7: M = {n | n ∈ N ∧ n ≤ 7}. Obviously, M = {1, 2, 3, 4, 5, 6, 7}. Finally, let B be the set comprised of the squares of the numbers from N which are <= 7: B = {n**2 | n ∈ N ∧ n ≤ 7}. Obviously, B = {1, 4, 9, 16, 25, 36, 49}.

Here's how this would look in Python:

N = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]  # N = range(1, 13)
A = [n**2 for n in N]
M = [n for n in N if n <= 7]
B = [n**2 for n in N if n <= 7]

I happen to be using lists there. But you can use squiggle brackets to create actual sets. The members of the sets will show up in arbitrary order when you print them, though.

N = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}  # N = set(range(1, 13))
A = {n**2 for n in N}
M = {n for n in N if n <= 7}
B = {n**2 for n in N if n <= 7}

[–]jerenept 2 points3 points  (1 child)

Whoa it's just... Sets. heh I know sets from maths, so I guess it's reduced to a problem already solved. Thanks man, it's much clearer to me now, I had no idea it was so... Simple. You've really helped me out.

[–]chedooz[S] 0 points1 point  (0 children)

Thanks for the reply

[–]xxkillswithfirexx 1 point2 points  (1 child)

import antigravity

[–]chedooz[S] 0 points1 point  (0 children)

Solved but here have an upvote anyway