Probability of guessing a number correctly in x guesses?
It only takes a minute to sign up. I want some recommendations or something I could add to the game. Please tell me what I could do better or what I did wrong. I made this during the time I learned Python. Right now, as it stands, you're just converting any user input to a integer, using the int function. What do you suppose happens if the user enters something like "abc"? What you need to do is set up a try - except block, like this:.
Rather than printing a message saying that if the user got below a certain amount of tries, they win, you can implement it into the code. The easiest way to do this would be to use a for This is not a very extensible design, again, I'd recommend creating a function that allows you to create custom games, like this:.
Why are you splitting this on to two different lines? You can just merge the int onto the line above it, and pass input into it like this:. As Greg Hewgill mentioned in the comments, rather than saying this:. This can be done by using a simple counter for how many times the user has made an attempt. Then, every time the code loops they enter an answeryou just increment the counter:. Then, at the end of your code and after the loop, you just need to set up some simple conditionals that check the attempts variable:.
Then, to take this step further and reduce magic numberscreate a constant at the top of your code that defines the number of attempts at which the user has lost. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Guess a random number between 1 and Ask Question. Asked 4 years, 11 months ago.
Active 1 year, 7 months ago. Viewed 26k times. Keanu Jones Keanu Jones 1 1 gold badge 3 3 silver badges 8 8 bronze badges.
Active Oldest Votes. This has been implemented below, for reference. You lost. Ethan Bierlein Ethan Bierlein Also I'd reveal the correct number if the player loses. In this case, the try: except: else: construction, putting the if: elif: elif test in the else clause, would fit well in the initial example, the input call should be outside the try clause on the same grounds.
An alternative to else would be to use continue within the except clause, which also saves an indentation level by an early exit on invalid input note the above comment on handling non-integer convertable inputs, though.
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It only takes a minute to sign up. Why is it that a number selected at random between 1 and can be determined in 7 or less guesses by always guessing the number in the middle of the remaining values, given that you're told whether your previous guess was too high or low?
Here's a link to the earlier thread: Guessing number between always can always be guessed in 7 guess. If you got it right: great. Continue in this way: cut your current interval of possibilities in half by a guess, so that you are either right or you can discard roughly half of the possibilities based on the announcement of "higher" or "lower". In your seventh guess, assuming that you're unlucky enough to have not guessed it yet, there's only one number that could possibly be it.
Irrespective of the answer being yes or no, you know the digit once the question has been answered. Let's start with a smaller number: Why 15? Your first guess is 8, as it is halfway from 1 to In general, for problems like this, you want to make your guesses so it doesn't matter which answer you get-that gets you the most information.
Whether you are high or low, you will have a range of 7 left. Let's say you were low, so the range is now 9 to Guess 12 and you will have 3 left either way. Now guess 10 or 14 for your third guess and you will get it on the fourth try. If you know about binary numbers, your first guess gets the leading bit, your second gets the second bit, and so on. I had the same problem for a program I was writing and I wasn't satisfied with these solutions because the set of possible questions was still very large and depended on the previous answer.
So here's an example solution for guessing a letter from the first eight letters of the alphabet. The questions are always the same. If you test these three questions you will see that they always suffice in guessing the right answer for guessing a letter from A to H. If you can't figure out why it works, here's a clue. Write the binary number for each letter of the alphabet under each letter. A isB isetc. H is in this case. Then compare those numbers to the three sets above. Sign up to join this community.
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. This is my attempt at writing a guessing game. Where can I improve my code to make it more succinct?
Link to Revision 1 Guess a number between 1 and revision 1. You are triggering logic in the constructor? Never, ever do class logic in the constructor! The constructor is for creating an object only. In this class, you shouldn't even have a constructor. What are those doing as local constants? Those should probably be fields. They aren't specific to a single method, the whole class uses them.
Wait a sec--why does your game player know when the program ends? What if you had two games, and the user was just exiting this game? Only Main should know when the program ends. Make a field for Random and only assign it once. Each time you use it, it reseeds itself so it will really be random. You are using the local clock to seed it here, so it will product the same value if you call it twice in a single second. In HigherOrLower int differenceyou should just return the result directly from the switch.
Don't assign a variable, then return at the end of the method. Sure there is. Check the input and notify the user, and let them try again. And do it in a dedicated method. Maybe you can modify the stub GetInput method I gave you into GetInput int minValue, int maxValue and make sure it is within the ranges provided. This is a perfect example of extracting similar logic into a more generic function. Parse will throw an exception if the user types a non-number.
Use int. TryParse to see if it is a valid number, and make the user input a valid number. There appears to be a lot more stuff I don't have time to review.
However, this should provide you plenty to chew on for now.
Random Number between 1 and 100
In your ContinuePlaying method, a switch block would work better, something like this:. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered.
Guess a number between 1 and Ask Question. Asked 3 years, 3 months ago. Active 3 years, 3 months ago. Viewed 3k times. WriteLine "Welcome to the numberline game" ; Console.Hot Threads. Featured Threads. Log in Register. Search titles only. Search Advanced search…. Log in. Contact us.
Each time Bob randomly guesses the number, Amy tells him whether his guess is too low, too high, or correct. Given that information, what is the probability that: 1 it will take Bob at most 6 guesses 2 it will take Bob at most 30 guesses Homework Equations The Attempt at a Solution Well, this is not really a solution, but let me tell you what I know about the problem so as to help someone give me a solution: The probability of guessing the number correctly changes, because Bob will always change his guess based on Amy's clues.
For example, if Bob guesses 36, and Amy tells him that guess is "too high," Bob then knows that Amy's number is somewhere between 1 and Last edited: Jun 13, HallsofIvy Science Advisor. Homework Helper. The best thing Bob can do is "bisect". That is, guess "50" to start with then, being told "too high" or "too low", guess the middle of the remaining segment of numbers. Think about what the tells you. HallsofIvy said:. LCKurtz Science Advisor.
Insights Author. Gold Member. Bob doesn't want to "bisect," he likes randomly guessing the number. Adam D. The problem says, "Each time Bob randomly guesses the number, Amy tells him whether his guess is too low, too high, or correct.
So assume that Bob guesses totally randomly bounded by his understanding of the solution space the minimum and maximum it could beand that Amy has chosen a border case number like 1. Amy chooses 1.
Bob unluckily guesses a number N1 between Amy probably says "too high. Adam D said:. Log in or register to reply now!For even more edification, read this article in German: Denken Sie an eine Zahl zwischen 1 und Well, the whole setup is a game. But ignoring that, no. Anybody can see that.
What kind of statistician are you, anyway? Still, I insist. There is no probability. Look, you coulda picked 1, you coulda picked 2, and so on up to That makes one outta ten.
Last I checked. Meaning the words and grammar of any probability problem matter. Hang on. You said 6. How could you even pick if you had to first select from an uncountable number of numbers? Good question. Funny man.
So you have to pick a non-integer, just to be a funny man. That not everything has a number is a hard equation to swallow for some, growing up as they do, devoted to scientism. The only proof there is that everything has a number is hope. As in so many things, Thomas Berger has the best thing to say about our obsession with numbers. Numbers, numbers, I had forgot how important they was.
As important as they are, they are not the most important things. Quality triumphs over quantity. View all posts by Briggs. It consists of numerous trite answers and is repeated ad nausem in the hopes of winning through intimidation. Nitpicking is one chapter. It reads like you are walking the fine line between something having no probability and something having zero probability. In your example, the guesser should have a chance at guessing the right number. Calculating the probability of getting it right, given the premises offered, should tell us that there are an infinite uhgg number of possible answers, each just as valid as the others.
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It only takes a minute to sign up. Here is the problem.
The player tries to guess that. If the guess is larger than true value, the player is punished by 'a' dollar. If the guess is smaller than true value, the player is punished by 'b' dollar. How much money the play should prepare in order to hit the number in the worst case? Here is where I am so far. For the first sub-question, the player would use binary tree and the worst case would be 7 guessing times 6 wrong and 1 right. For the second sub-question, instead of separating the range by half, the player will separate the range according to the weight given by a and b, i.
In this strategy, the worst case would cost the player 11 dollars according to my calculationwhile the binary method would cost the player 12 dollars.
Pick A Random Number From 1-10
For the third question, the methodology is the same as second one. My doubt is: is there another method which is better?
The doubt comes from this fact: my method applied same to sub-question 2 and sub-question 3, which means the existence of sub-question 3 is meaningless. Clearly an author of a problem would not put that kind of sub-question.
Then we have. This can easily be solved by hand for the numbers in question, or a general technique for solving recurrences can be applied. I leave the other cases to you to verify you don't spend more. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 2 years, 6 months ago. Active 1 year, 1 month ago. Viewed times. Just to express my opinion which is probably a reference for others, I would prefer the answer using recurrences since the logic is more universal and the solution is mathematically rigorous.
Proper integer conversion
It only takes a minute to sign up. Say I'm thinking of a number between 1 and At the end we will know both parts of the outcome. This does not make the problem harder: if we have a strategy that produces the secret number, we'll be able to deduce the lie-position too simply by comparing the answers to what they should have been.
We don't need to tell him that's an option, but it makes the strategy slightly easier to describe if we imagine this is the case. Can we do that, however? Still unsolved: Is it possible to get through if we have to ask all our questions simultaneously without seeing any answers and the questions cannot explicitly refer to which answers are provided for other questions or where the lie is?
Intuitively I doubt it; that would need an extremely efficient packing of groups of 10 points in the dimensional hypercube of possible answer sequences. This variant variant has gained some attention in the comments and in other answers. I think it will shed some light on the original question, so I'll give an answer here.
Is it possible to get through if we have to ask all our questions simultaneously without seeing any answers and the questions cannot explicitly refer to which answers are provided for other questions or where the lie is? So any strategy to the original question must adapt the questions according to the answers we have got. When we write down the answers, we get another binary word, and since there is exactly one lie, we know that the word differs from the codeword of the correct answer in exactly 1 position.
In what remains, the distance between two codewords is the number of positions in which they differ. Here is an example. Other numbers will match for on average 5 numbers, since Sha models a random oracle.
Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 2 years, 6 months ago. Active 2 years, 6 months ago. Viewed 3k times. How can this be done?
Richard Richard 7 7 silver badges 12 12 bronze badges.Finding Prime Numbers
The whole "you can't use LEM" is probably meant to say, "Is the number greater than 50" "No" Then you can't infer that it is less than 50 LEM because you don't know if that answer was a lie.