• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

The random target number is supposed to change but stay the same in my little Go program

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 158
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It generates only one random number at the start. So, of course, it will stay the same throughout the life of the program.
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had that program working yesterday showing the guess number and the random different target number. But for some reason I re-wrote the program and now it shows the same random number. I know it's possible because I did it yesterday. I just forgot something in the process.
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I just found out! The : target := rand.Intn(100) + 1 needs to be placed after :fmt.Print("Make a guess :"). That way the random func runs each time I enter a guess therefore I have a new random target number !

 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not working properly it keeps saying my guess was too low even when it is too high.
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It works fine but it only runs once instead of giving me 10 guesses[/code]
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok the program now works fine and give me a different random number each time I enter a new guess.
It just saying that my guess number that I enter is too low instead even though my guess number is sometime higher than the random number.
Any idea where the bug is ?

 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry to insist and find out from you guys where is the bug. I am just scared if I don't get the answer quickly I will give up again programming and come back on this board in 6 months. So if you know the bug please let me know , I have tried to fixed this issue for 3 hours now , I did improve the program in the post to have the program comparing my guess number to a different random number each time but for some reason it does not verify the if statement (too low or too high, and tells me it is too low even though when my guess is higher than the target number). Thanks for your patience guys and your kind replies.
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok guys forget it ! I just try the program again and it is working properly. I have no idea why it was giving me the guess too low each time I was running it. Now it works perfectly fine even though I did not change anything to the code. That's really weird ")
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok guys I am trying the program again and now it is showing the same random number. It works 5 minutes again perfectly well now it is not. What's wrong with this program? I have changed NOTHING to the code
 
Rancher
Posts: 3787
49
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why your code seems to change behavior when you don't think you've changed it.  May I suggest, don't panic.  Study carefully what the program is doing.  Is it possible you're running different versions of code?  Do you have just one file for this code, or multiples?  What command are you using to run the code?

When I run your code, it seems to run reasonably well.  The things I notice are: if you guess correctly, what do you want to do?  I see code for what happens if a guess is too low, or too high.  Should anything happen if you guess correctly?  If so, put in code for that.

I don't know just what you intend to do.  If I am playing a game to guess a number, and I guess a number and it's too low, I would expect that on my next guess, I will be trying to guess the same target number.  If my last guess was too low, my next guess should be higher.  So I would expect to keep guessing until I get it right, or until I use all 10 guesses.  Is that what you intend this game to do?

I think probably, you need to only change the target number once at the beginning, and once each time the guesser guesses correctly.
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike and thank you so much for your reply. Yes this was piece of code was program from a book but I decided to add much more functionality to it as a challenge but now
it is working well with normal behavior. As you said it is a game and the goal is to have the guess input matching with the random target number. . I added as you suggested
an extra line of code in case the guess and the target match. Thanks again for your help as nobody will help me yesterday I was forced to worked on it on my own, I guess
that's the way you learn I need to mention that's my second program in go as I am a total beginner. I will post the final result again for the one who are starting go as I do
and could possibly learn from this example.

 
Mike Simmons
Rancher
Posts: 3787
49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred.  I understand; we were all beginners at one point.  I think you need to look carefully at where your blocks start ({) and end (}).  Note that your new code that says "if guess == 100" is inside a block that says "if guess > target".  So if guess is equal to target, execution will not go into that black, and it will never check to see if guess == target.  I think you need to move that if statement outside of the block it's currently in.
 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike for your suggestion but I did change a little bit the code to make easy to see if the guess = target . I use success := false and I introduce break in case it is a success=true ,  if !success I print each time it is
unsuccessful:  the sorry you did not guess my number it was ", target. It does repeats the  target number each time but it will break if success is true. I did try it a few times and I found the target number and it stop at the 6th try.
here's the code :

 
Fred Masen
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
 
reply
    Bookmark Topic Watch Topic
  • New Topic