Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Generating a random number, using a variable & specified value's

Author
Message
MenTalTerYn
14
Years of Service
User Offline
Joined: 27th Dec 2009
Location: In my chair
Posted: 4th Jan 2010 01:01
Okay, so I'm takin' a whack at making my own number guessing game, but I've hit a road block. I have the basic game done, but I tend to want to add more onto what I do. I came up with an idea to have the computer try to guess the players number instead of the other way around. What i was trying to do was make the player enter his number, store it as a variable, then have the computer generate a random number between 1-100. Then compare it's guess with the variable, narrow it's value and then try again, until finally getting the answer.
My problem is that I can't seem to figure out how to change the values for the random number to be generated. Lets say my number was 20, and the computer guessed 40. Well 40 is too high, so I want it to narrow it's values down and generate a new number. So instead of 1-100, it'll be 1-39. This is where my problem comes in. I need to find out how to use the number the computer previously generated, then use it as the min/max value for the next number to be generated.
I've tackled this problem myself for a while, but to no avail. Any help would be greatly appreciated, and sorry for the long post.

This is what I got so far, though it's no where near done.
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 4th Jan 2010 11:54
Well, I'd reccommend setting up two additional variables; min and max, storing the minimum and maximum values in the "guessing range".

Then you'd have guess = rnd(min + max) - min.
Compare it to the actual value like so:



Hope that helped somewhat

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 4th Jan 2010 12:09
I've run your code,are you asking the computer to find the first number given or to find the next number you guess each time.

so say I give it 35 it guess wrong it then asks me to input another number.
I thought it was to work out my first number and show how many guess it took.

Dark Physics makes any hot drink go cold.
MenTalTerYn
14
Years of Service
User Offline
Joined: 27th Dec 2009
Location: In my chair
Posted: 4th Jan 2010 18:44
Rudolpho, you make it seem so easy, lol. Never would've thought of that (still new to the coding scene, heh). Thanks a ton for the help!

Howdo, that segment of code wasn't completed yet, so I'm sure it had a few bugs in it. The basic idea was to have the computer guess your number until it finally guesses correctly, but since I was having problems coming up with a proper mechanic to narrow the computers value I wasn't able to finish it.

Thanks for the speedy reply guys, nice to know I have a place to go for help.

Login to post a reply

Server time is: 2024-11-24 20:18:29
Your offset time is: 2024-11-24 20:18:29