HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Simple random number generator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomnumbersimplegenerator

Problem

I have been wanting to learn programming for some time now and decided to really give it a shot today. I have edited and read lots of things in Java to fix small issues but never written from scratch. I've tried C and Java but both seemed just a bit too much to teach myself as a first language, so I'm going with Python first.

Can anyone look through this little sample random number generator I made and point out anything I should be doing differently or correctly? It is executing without issues for me but I'm sure I've got extra nonsense in here. I just have all the prints to see a result of each step and I know this is not what a real random number generator would be like. I'm just testing and learning my way around different aspects of the language.

import random

x = int(raw_input("Please enter a number up to 100. "))
z = random.randint(40,100)
""" x and z can both be used below without errors -- testing/learning"""

randomNumber = random.randint(1,z)
print "first random",randomNumber

if 35 >= randomNumber:
    newRandom = randomNumber * .5
    newRandom = newRandom + 9
    print "second random " "%.f" % newRandom

elif 36 <= randomNumber <= 93:
    newRandom = randomNumber + 7
    print "second random " "%.f" % newRandom

else:
    newRandom = randomNumber
    print "second random",newRandom

Solution

Code organization

Rather than having your code in the global namespace, it's better to wrap it into a method, for example:

def gen_random_number():
    randomNumber = random.randint(1, z)
    print "first random", randomNumber
    # ...


And call the method like this:

if __name__ == '__main__':
    gen_random_number()


Magic numbers

Avoid using "magic numbers": the numbers 40, 100, 35, 93 are "magic" because it's unclear what they are and why. If you put them in capitalized constants near the top of the file with a good name, it becomes much clearer what they are and their purpose, for example:

LOWER_BOUND = 40
UPPER_BOUND = 100

z = random.randint(LOWER_BOUND, UPPER_BOUND)


For 40 and 100 it was easy enough to guess what they are and give these names. But for 35, 93 I'm not sure. If you had given them a name, I wouldn't have to wonder.

Formatting

There is an official standard Python style guide called PEP8. I suggest to read and follow that, it will make it easier for others to review your code. There is command line utility called pep8 that can detect coding style violations. You can install it with pip install pep8, and run it on your script (or even an entire directory) simply with pep8 path/to/your/code.

Your code reformatted and variables renamed to follow PEP8 would look like this:

random_number = random.randint(1, z)
print "first random", random_number

if 35 >= random_number:
    new_random = random_number * .5
    new_random += 9
    print "second random " "%.f" % new_random

elif 36 <= random_number <= 93:
    new_random = random_number + 7
    print "second random " "%.f" % new_random

else:
    new_random = random_number
    print "second random", new_random


Although PEP8 doesn't complain about this:

print "second random " "%.f" % new_random


There's no need to put a space between those two strings, so you can write it simply:

print "second random %.f" % new_random


You could further simplify this:

new_random = random_number * .5
new_random += 9


to this:

new_random = random_number * .5 + 9

Code Snippets

def gen_random_number():
    randomNumber = random.randint(1, z)
    print "first random", randomNumber
    # ...
if __name__ == '__main__':
    gen_random_number()
LOWER_BOUND = 40
UPPER_BOUND = 100

z = random.randint(LOWER_BOUND, UPPER_BOUND)
random_number = random.randint(1, z)
print "first random", random_number

if 35 >= random_number:
    new_random = random_number * .5
    new_random += 9
    print "second random " "%.f" % new_random

elif 36 <= random_number <= 93:
    new_random = random_number + 7
    print "second random " "%.f" % new_random

else:
    new_random = random_number
    print "second random", new_random
print "second random " "%.f" % new_random

Context

StackExchange Code Review Q#58165, answer score: 16

Revisions (0)

No revisions yet.