Public Group

# guess the number variant (python)

This topic is 4255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to make a variant on my guess the number program. Instead of the user guessing the computers number, I want the computer to guess the users. Im guessing I need to have the computer generate random numbers but limit the them to whether the user says "higher" or "lower". I know the random module but the command random.random() generates a number between 0.0 and 1.0 I need it to generate whole numbers. Can anyone give me some tips?

##### Share on other sites
Well, I know nothing about python, but if you can control the precision of the numbers generated by the function, then you can just multiply by a variant of 10 to get a whole number.

For example, if it generates 0.253, multiply by 1000 to get 253. Of course, this could only work if you can control the precision of the numbers generated(I don't know this function, so i don't know if you can).

Although, even if it is a set presicion, as long as it's high enough, you can still make it work, just multiply to get the whole number in the range you want, and ignore the rest of the decimals after the point.

##### Share on other sites
You could use the randint() function in the random module.

From the help page:

randint(self, a, b)    Return random integer in range [a, b], including both end points.

If your feeling masochistic, or just want to know how to convert a float to a whole number (integer), you could use the "int" function to convert ANY variable to an integer.

print int(0.123)        # prints 0print int(0.123 * 100)  # prints 12print int("50")         # prints 50print int("oh no!")     # raises an exception

Welcome to Python!

- Mike

##### Share on other sites
Thanks alot.. I wasnt aware of the randint() function. I think that would be perfect for what im trying to do. Would it be the best way to have the computer guess a user defined number? I was planning on having the range of the computers guess start at 0 to infinity, then change the range of the randint() based on the users response of "higher" or "lower".

##### Share on other sites
I don't think its possible to generate a number between 0 and infinite. You'll need to decide on a number range that the user would choose from. This could be very large, though. Python provides easy support for massive numbers:

import randomprint random.randint(0, 999999999999999999999999999999999999999999999999999999999999999999999999999999999)

As for your algorithm, I can't think of any better way for the computer to guess the player's number than what you've already mentioned. I don't see any way that the computer could find a completely random number better than that.

##### Share on other sites
Besides magic or mind-reading. Both of which require a better computer than the ones currently available. [smile]

##### Share on other sites
Yay, its nice to know that I thought of the best algorithm for my problem :) makes me feel like Im actually learning hehe. thanks guys

##### Share on other sites
Your method will work, but I'm afraid it's not the best way (although it will on some occasions perform better than the best way). The best solution in this case would be a Binary Search; the example on the linked page is actually talking specifically about the problem you're trying to solve. [smile]

As a learning excercise you may as well try implementing both solutions though, just for a bit more practice. You could then actually try both out a few times and see for yourself that the binary search will usually (but not always) get the number with less guesses than your own solution.

##### Share on other sites
Isn't biggjoee5790's algorithm basically a binary search? My understanding was that the higher/lower input from the user would be used to divide the range of possible values in half, until the correct value is guessed.

Quote:
 from the Wikipedia article:An example of binary search in action is a simple guessing game in which a player has to guess a positive integer, between 1 and N, selected by another player, using only questions answered with yes or no. Supposing N is 16 and the number 11 is selected, the game might proceed as follows. * Is the number greater than 8? (Yes). * Is the number greater than 12? (No) * Is the number greater than 10? (Yes) * Is the number greater than 11? (No)

Am I missing something?

##### Share on other sites
Quote:
 Original post by doctorsixstringIsn't biggjoee5790's algorithm basically a binary search? My understanding was that the higher/lower input from the user would be used to divide the range of possible values in half, until the correct value is guessed.
Not quite. I believe biggjoee5790's algorithm was to generate a random number between the lower and upper bound and see if it's correct; if not, he uses the 'higher' or 'lower' to change one of those bounding numbers and then generates another random number within a new range. In this solution the range will not always (or even often) be halved.

The binary search on the other hand will guess the exact midpoint of the range each time, halving the remaining range. You can easily spot that the two methods are different based on the fact that the binary search doesn't actually require any random numbers.

Just for clarity, let's look at an example search with each algorithm. We'll look for a number between 1 and 10, inclusive (I'm not going to repeat that each time, but from now on any number ranges mentioned are inclusive). For the purposes of our example the number will be 7.

biggjoee5790's method:
1. The number is between 1 and 10, so we generate a number between 1 and 10. We'll say our random number was 1. 7 is higher than 1.
2. We now know the number is between 2 and 10. Our random number is 8. 7 is lower than 8.
3. We now know the number is between 2 and 7. Our random number is 4. 7 is higher than 4.
4. We now know the number is between 5 and 7. Our random number is 6. 7 is higher than 6.
5. We now know the number is between 7 and 7. Our random number must be 7, which is the correct answer.

binary search:
1. The number is between 1 and 10, so we guess the midpoint (we'll round down if neccesary since we only allow whole number guesses). The midpoint is 5. 7 is higher than 5.
2. We now know the number is between 6 and 10. The midpoint between these is 8. 7 is lower than 8.
3. We now know the number is between 6 and 8. The midpoint between these is 7. This is the correct answer.

So, in this case the binary search took 3 guesses to get the correct number, and biggjoee5790's algorithm took 5. Note that it's entirely possible for biggjoee5790's random numbers to guess every value less than 7 in this example, which would have taken 7 guesses to find the solution; it may also have guessed 7 on the first try.

Therefore, the binary search is generally more efficient, but may not always be as fast. You can probably also see by looking at the examples that the chance of biggjoee5790's algorithm getting the answer in less guesses decreases significantly as the range of numbers gets larger; finding a number between 1 and 100 would probably (but not definitely) take a lot more guesses.

Again, I think implementing both solutions would be a good excercise, and both will correctly solve the problem.

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633310
• Total Posts
3011304
• ### Who's Online (See full list)

There are no registered users currently online

×