Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Mauhan · Mar 14, 2016 at 09:52 PM · randomfloatnumbergenerateother

Random float but not the same

Hello guys. I trying to make tap game namely: Player have 4 cubes on screen, randomly colored, and he must tap if it is colored. I maked maaaaany random generators, I looked the same ways but I don't have a idea ;c

So.. my last code look like this:

     public float newNumber;
     public float lastNumber;
     public float finalNumber;
 
     //its only for preview
 
     void randomGenerator()
     {
         newNumber = Random.Range(1, 4);
         lastNumber = newNumber;//its in update
         if (newNumber != lastNumber)
         {
             finalNumber = newNumber;
             print(finalNumber);
         }
         else 
         {
             //generate one more time
         }
     }
 

I want in order to generated number never be printed if it is the same like previous. I know, my English is low but if you don't know something , tell me. Above code not working because script generating number longer time. Thank you in advance guys:')

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LazyElephant · Mar 14, 2016 at 11:43 PM

You're assigning lastNumber to equal newNumber directly before the if statement which tests that they're equal. This will cause the if statement to always be true. The following code should work to give you a non-repeating random number.

  void randomGenerator()
  {
     do {
        newNumber = Random.Range(1, 4);
     } while (newNumber == lastNumber);

    lastNumber = finalNumber = newNumber;
  }

I don't see a reason to even use finalNumber, but I left it in since you have it in your code. Having a variable to hold the newNumber and lastNumber should be enough.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mauhan · Mar 15, 2016 at 03:09 PM 0
Share

Thank you, it;s really helpful. I must learn do and while so, thaaank you:')

avatar image
1

Answer by RudyTheDev · Mar 14, 2016 at 11:29 PM

Something like this:

 public float GetDifferentRandomInRange(float from, float to, float notValue)
 {
     float value;
     while ((value = Random.Range(from, to)) == notValue) { }
     return value;
 }

For clearer idea of what it is doing:

 public float GetDifferentRandomInRange(float from, float to, float notValue)
 {
     float value;
     do // Keep doing this:
     {
         // Generate a random value
         value = Random.Range(from, to);
     }
     while (value == notValue); // until the value isn't the same as the last
     return value; // last generated value is good to go
 }

I think you want to use int instead of float though, since you are talking 4 or however many discreet cubes.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mauhan · Mar 15, 2016 at 03:08 PM 0
Share

Thank you so much for help:')

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generate a random number different to the last 4 Answers

PLEASE TELL ME WHAT IS WRONG HERE I RECIEVE THIS ERROR 1 Answer

Can I generate values outside of the Random.insideUnitCircle?? 1 Answer

randomly shoot using number generator 0 Answers

How to randomize a stages with no repetition? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges