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 /
avatar image
0
Question by MrFieryIce · Nov 30, 2014 at 09:32 PM · javascriptphysicsmath

How can I create a random integer value that is a common multiple of two other variables?

I am working on a very math and physics based game (for a "small" school project), where the player would have to build the best rocket possible (best acceleration and/or speed with distance) using 2 parts: the thrusters and fuel tanks. I want to setup a small money system, where the player has a certain amount of money to buy these parts. I'm trying to figure out how to make the "budget" value, determined by a common multiple of the two parts (if that makes any sense). Say I had 210k dollars, the thruster costs 30k and fuel tanks 70k. I could either buy 7 thrusters and 0 fuel tanks, 5 thrusters and 1 fuel tank, 3 thrusters and 2 fuel tanks, OR 0 thrusters and 3 fuel tanks. I want to make a program, that randomly sets the "budget" value/variable in integers to be some common multiple of two other "cost" variables. (I know this is not exactly "true" rocket physics, but I'm partially limited on time, and it'll give me more of a headache to try and implement all variables into the 2D game. If you can create a piece of code, I would greatly appreciate it if it was in JavaScript, but anything is really fine. Thank you in advance.)


Edit : I'm sorry for the partial confusion I might have caused. I want the variables "budget", "fueltankPrice" and "thrusterPrice" to be randomized, but the "budget" variable must be a common multiple of the other two. So if I had 16 dollars in my budget, the fueltankPrice and thrusterPrice must either be something along the lines like 2 and 8; 4 and 4; 8 and 2... etc. etc. This all happens when I start playing/load the scene.

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 Kiwasi · Nov 30, 2014 at 11:00 PM 0
Share

If your in a hurry you coul just buy kerball ;)

avatar image MrFieryIce · Nov 30, 2014 at 11:24 PM 0
Share

I think plagiarism would slap me in the face if I showed it at my little demonstration math/science "fair".

2 Replies

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

Answer by jenci1990 · Nov 30, 2014 at 10:43 PM

If Press "A" Debug the result.

 var myMoney : int = 210000;
 var thrustersPrice : int = 30000;
 var fueltanksPrice : int = 70000;
 
 function GetRandomCount(money, price : int) {
     var returnValue : int = money/price;
     returnValue = Random.Range(0,returnValue);
     return returnValue;
 }
 
 function Update() {
     if ( Input.GetKeyDown(KeyCode.A)) {
         var thrustersCount : int = GetRandomCount(myMoney, thrustersPrice);
         var newMoney : int = myMoney - (thrustersCount * thrustersPrice);
         var fueltanksCount : int = newMoney / fueltanksPrice;
         newMoney = newMoney - (fueltanksCount * fueltanksPrice);
         Debug.Log("Thrusters: " + thrustersCount +"   "+ "FuelTanks: " + fueltanksCount +"   "+ "Money: " + newMoney);
     }
 }
 
Comment
Add comment · Show 8 · 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 MrFieryIce · Dec 01, 2014 at 12:55 AM 0
Share

How could I randomize the first three variables in that code?

avatar image MrFieryIce · Dec 01, 2014 at 01:11 AM 0
Share

I also get an error saying: "Assets/LaunchScript.js(8,35): BCE0051: Operator '/' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'."

avatar image jenci1990 · Dec 01, 2014 at 01:15 AM 0
Share
 var my$$anonymous$$oney : int = 210000;
     var thrustersPrice : int = 30000;
     var fueltanksPrice : int = 70000;
     function GetRandomCount(money : int, price : int) {
         var returnValue : int = money / price;
         returnValue = Random.Range(0,returnValue);
         return returnValue;
     }
     function Update() {
         if ( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)) {
             var thrustersCount : int = GetRandomCount(my$$anonymous$$oney, thrustersPrice);
             var new$$anonymous$$oney : int = my$$anonymous$$oney - (thrustersCount * thrustersPrice);
             var fueltanksCount : int = new$$anonymous$$oney / fueltanksPrice;
             new$$anonymous$$oney = new$$anonymous$$oney - (fueltanksCount * fueltanksPrice);
             Debug.Log("Thrusters: " + thrustersCount +" "+ "FuelTanks: " + fueltanksCount +" "+ "$$anonymous$$oney: " + new$$anonymous$$oney);
         }
     }
 
avatar image jenci1990 · Dec 01, 2014 at 01:17 AM 0
Share

:) its ok

avatar image jenci1990 · Dec 01, 2014 at 01:19 AM 0
Share

for Randomite firs three variable use this :

 function Start() {
     // Randomize when Start Game
     my$$anonymous$$oney = Random.Range(0, 500000);                 // Random $$anonymous$$oney Between 0 and 500000
     thrustersPrice = Random.Range(5000, 30000);         // Random Price Between 5000 and 30000
     fueltanksPrice = Random.Range(10000, 70000);     // Random Price Between 10000 and 70000
 }
Show more comments
avatar image
0

Answer by Kiwasi · Nov 30, 2014 at 11:22 PM

I'm missing something here. What's wrong with this?

 int budget = noOfRockets * rocketPrice + noOfFuelTanks * fuelTankPrice;

If you like you could randomise the noOfRockets and noOfFuelTanks.

 noOfRockets = Random.Range(0,10);
 noOfFuelTanks = Random.Range(0,10);
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Collision impact force 1 Answer

How do I switch from Character player to Airship Vehicle using triggers? 0 Answers

getting the lowest point of a sine wave in javascript 3 Answers

How do I "remove/disable" collision? 3 Answers

Hop (Ketchapp) Mechanic (Jump between platforms with certain velocity while controlling object on X axis) 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