Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Aug 29, 2014 at 05:35 PM by robertbu for the following reason:

Not a Unity Question

avatar image
0
Question by basseshigh · Aug 29, 2014 at 05:35 PM · randomgenerationdivision

Division Sum Generator problem

I've got a sum generator for an educational game I'm working on, I've got it working for addition, subtraction and multiplication but I'm having trouble coming up with a division sum generator, I need a way of making the second number a divisor of the first number, I'm just not sure how to go about doing that, any help is appreciated. Here's my existing code:

     //Plus Problem Setup
     void Plus()
     {
         Sign.text = "+";
         number1 = Random.Range (1, 11);
         number2 = Random.Range (1, 11);
         answer = number1 + number2;
     }
 
     //Minus Problem Setup
     void Minus()
     {
         Sign.text = "-";
         number1 = Random.Range (1, 21);
         number2 = Random.Range (1, number1);
         answer = number1 - number2;
     }
 
     //Multiplication Problem Setup
     void Multiplication()
     {
         Sign.text = "x";
         number1 = Random.Range (1, 11);
         number2 = Random.Range (1, 11);
         answer = number1 * number2;
     }
 
     //Division Problem Setup
     void Division()
     {
         Sign.text = "÷";
         number1 = Random.Range (1, 21);
         number2 = Random.Range (1, number1);
         answer = number1 / number2;
     }

Comment
Add comment · Show 2
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 robertbu · Aug 29, 2014 at 05:36 PM 0
Share

Not a Unity Question. You can do it this way:

 do {
     number2 = Random.Range (1, number1+1);
 } while (number1 % number2 != 0);

Note I'm assu$$anonymous$$g eventually this code will find '1' if the number is prime, but given computer Random, that may not be a sure thing. I always hesitate to put in any code that has the potential to hang. It might be better to make an attempt for some fixed number of trys, and if it fails, generate a new Random number:

 int i = 0;
 do {
     i++;
     if (i > 1000) {
         number1 = Random.Rangle(1, 21);
         i = 0;
     }
     number2 = Random.Range (1, number1+1);
 } while (number1 % number2 != 0);


Note on the 'number1+1'. Unity Random.Range() is exclusive on the final value (i.e. it does not generate it) for integers. If I did not do that, and your number1 was assigned 1, then your code would hang.

avatar image Scribe · Aug 29, 2014 at 06:30 PM 0
Share

I know this has been closed but just to point out, you could do this inversely by working out number1 based on a random pick of number2 and multiplying it by something, then you know for sure if divides to give 'something':

 number2 = Random.Range (1, 21);
 number1 = Random.Range (1, $$anonymous$$athf.CeilToInt(21f/(float)number2))*number2;
 answer = number1/number2;

Scribe

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Best way to render a list of rooms 0 Answers

Randomly Generated Objects inside an area 3 Answers

Mapping Cube to Sphere - Using 64 planes as a cube "side" 1 Answer

More True Randomess - RNGCryptoServiceProvider explanation required 1 Answer

How to change the origin of a gameobject? 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