About 'Use Random.Range()'!


In this video (objectives)…

  1. Use Random.Range() for our guess to introduce randomness.
  2. Refactor our code to only have 1 place of calculating guess.
  3. Tweak our logic to prevent the computer from guessing a number already guessed.

After watching (learning outcomes)… Use Random.Range() to add randomness to the number guessed.

(Unique Video Reference: 10_UI_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!


Heya there Rick,

I believe there are 2 issues in this video( bash me if I’m mistaken)

First is that using Random.Range with integers is sightly different than using it with floats. The thing is that “max” is not included, its excluded from the generation process:

Second is what happens if you keep pressing Higher? In my case(and in doubt I copied all the code from the github link) it goes over 1000.

Check this please:

I got arround this little bug by adding a condition that only +1 the min if it does’t go above the max(the same for max in lower button)

Thank you for your time,

Already guess number bug

Oh goodness, you are correct! Max is exclusive for integers, its only inclusive for floats. Thanks for pointing that out.

In terms of the value going over, I noticed this and it requires the player to say higher if the number is actually correct which I figured is not logical. So, if your number is 1000, the guess is 1000, you got it correct, but pushing higher will produce a guess of 1001.

We can also clamp the value, but I didn’t want to introduce that just at the moment.


I’m uploading a patch for the video now, thanks again for pointing this out!


Perhaps I am wrong, but if in the StartGame() there is a max=max+1 (so the program arrives at 1000) before calling NextGuess(), then there is no need to do OnPressLower max=guess-1 to do then in NextGuest max+1. The random will begin between 1 and 1001 (1 to 1000), and the max will be always 1001 until OnPressLower() is called. Then max will be the guest and the program doesn’t need to do -1 in OnPressLower() following a +1 in NextGuess().

I think this explanation is the most confusing thing I have written this last week… :sweat_smile:


Well, I used your patch of max = guess - 1, and unfortunately it doesn’t fix the issue totally. If you keep pressing higher over and over, you might get to 1005 as I did. I don’t know the best fix however, as I am not a good programmer (yet!).

Edit: Actually, if you get to 1005 and press lower than higher several times, then lower, then higher again several times you can keep going up forever!


A game within a game!

We chat about this in upcoming videos, especially in an instructor hangout on this topic.