- Home /
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.
I think plagiarism would slap me in the face if I showed it at my little demonstration math/science "fair".
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);
}
}
How could I randomize the first three variables in that code?
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'."
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);
}
}
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
}
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);
Your answer
