Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time to get started on Python homework...
#1
Can I get some sort of jumpstart with a python script? I've been on this same project for over a year and can't for the life of me seem to get started on it..Frown

Code:
The assignment: You've been hired to write a program that will choose random lottery numbers for the Lotto 6-49. Your program should create a list of 6 different random numbers in the range of 1 to 49, then print them all out on one line.

He says it has to do with "structured data" in a program. Any help would be appreciated.
Reply
#2
import random

Then look up its documentations for RNG features.
Depending on the specific requirements (numbers repeating, non-repeating adjacents, such and such...), figure out an appropriate way to do it. By data structures, I assume your teacher/whoever wants you to use arrays and/or lists, which are what I'd use if the numbers don't repeat themselves.
As for printing everything out in one line, I *think* an efficient way is to concatenate the sub-results into a final string to be output. If I'm not mistaken, concatenating string in Python involves only a + operator.
Reply
#3
Is it okay for you to use pseudo random numbers for this assignment?

Overall, this sounds like a really really simple program, probably only a few lines of code unless I'm seriously missing something...
Reply
#4
Heidi Wrote:Is it okay for you to use pseudo random numbers for this assignment?

Overall, this sounds like a really really simple program, probably only a few lines of code unless I'm seriously missing something...

Do you have any other approach to generate random sequences of items? I'm curious how this can be done without an RNG. :f6:
Reply
#5
Jesus, I should have really started this when python was still fresh in my brain... None of this is making sense to me..
Reply
#6
Kalovale Wrote:Do you have any other approach to generate random sequences of items? I'm curious how this can be done without an RNG. :f6:

Obviously a RNG of some kind has to be used. Whether or not it is a PRNG is the question.

It could be possible to interface with something like this site
http://www.random.org/
Although I'd say that's beyond the scope of Alloy's assignment.
Reply
#7
Code:
import random
possibleNumbers = range(1, 50)
random.shuffle(possibleNumbers)
randomNumbers = [str(number) for number in possibleNumbers[0:6]]
print(', '.join(randomNumbers))
Reply
#8
Wow.. Now I feel retarded.
Reply
#9
Alley Wrote:Wow.. Now I feel retarded.

I'll try to dissect Fiel's algorithm.

 Spoiler
Reply
#10
range(1, 50) creates a range from [1, 50)

Here's a more Python noob-friendly way of doing it.

Code:
import random

randomNumbers = []
while len(randomNumbers) < 6:
    randomNumber = random.randint(1, 49)
    if randomNumber not in randomNumbers:
        randomNumbers.append(randomNumber)

for eachRandomNumber in randomNumbers:
    print(str(eachRandomNumber) + " ")
Reply
#11
Fiel Wrote:range(1, 50) creates a range from [1, 50)

Why are the endpoints so inconsistent...
Reply
#12
Python is a bit strange with endpoints compared to other languages. You'll just have to get used to that. :-)

And keep in mind that if you are unsure about what values will be in a list or something, such as endpoints, just experiment by typing commands into the python interpreter. Interpreters are great for learning and understanding programming languages.
Reply
#13
Experimenting is the spirit of learning this business anyway. It'd just be helpful if you can make reasonable assumptions here and there instead of having to rely on empirical experience.
Reply
#14
Fiel Wrote:range(1, 50) creates a range from [1, 50)

Here's a more Python noob-friendly way of doing it.

Code:
import random

randomNumbers = []
while len(randomNumbers) < 6:
    randomNumber = random.randint(1, 49)
    if randomNumber not in randomNumbers:
        randomNumbers.append(randomNumber)

for eachRandomNumber in randomNumbers:
    print(str(eachRandomNumber) + " ")

Yeah, that'll probably make my teacher less hard, thus less suspicious. Looks like something I would come up with, thanks Fiel Rolleyes
Reply
#15
I hate to double post, but I didn't want to start a new thread.

Here is the second part of this assignment:

Code:
Your previous lottery number generator would work for selecting Lotto 6/49 numbers but other lotteries use different sets of numbers and different lengths of lists. It would be much more useful if we could have a program that would let the user input the minimum and maximum values for the random generated numbers as well as the number of random numbers needed, like this:

>>> How many random numbers do you need generated?
5
>>> What is the minimum value of random number?
1
>>> What is the maximum value of random number?
20

>>> [2,17,4,12,9]

Write a program called lotto2.py that will get input from the user for the number of random numbers to be generated, the minimum value and the maximum value. It should include a function called "numlist" that will take those three values as parameters then return (*not print*) the list of random values.

Now my brainstorming got me to the point of using inputs in place of the min and max(1, 50) and the number of numbers(:6), I tired using variables for them, but I guess I'm too much of a noob to do any of this.. Also, I have no idea of where to use this "numlist" he speaks of.
Reply
#16
numlist is just a function you have to write...

Were you taught about this stuff in the class you were given the homework in? This is very basic stuff here.
Reply
#17
Not at all. He gives us these very long pages to read, and really none of it coincides with the homework.
Reply
#18
So does it mention about what functions, parameters, return values, etc are?
Reply
#19
Heidi Wrote:So does it mention about what functions, parameters, return values, etc are?

Considering the pages being "very long", I'd assume yes. Who could write pages on introduction to programming without those concepts?
Anyway, this is how you do it:

Instead of explicitly declaring the values like Fiel did
here
Code:
while len(randomNumbers) < 6:
and here
Code:
randomNumber = random.randint(1, 49)
, you implicitly refer to those numbers via variables:
Code:
while len(randomNumbers) < totalLotteryLength:
and
Code:
randomNumber = random.randint(1, UpperRandomNumberLimit)
.

Where do you get these numbers? From the user, as instructed. So ask the user for the inputs and store their answers into the variables.
Reply
#20
Can I have two variable in randomNumber? As in:

Code:
randomNumber = random.randint(LowerRandomNumberLimit, UpperRandomNumberLimit)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)