Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Falnom

Checking a Random #

This topic is 5621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am building a console program that calculates and sort of simulates the effects that a rocket under goes as it travels up and through space. One of the first things I do is ask where the rocket will be launching from. One of the choices has the computer randomly chose from different planets (I only have 2 in place as of now: Earth and the Moon). I am having trouble with making it truly random. Well here''s the code for choice 4: case 4: srand((unsigned)time(0)); for(int index=0; index<1; index++) { random_integer = 1 + int(3.0 * rand()/(RAND_MAX+1.0)); rop = (random_integer * 1); } if (rop == 2) { planet = 1; cout << "\nYou have chosen Earth\n"; cout << "\t\tPlanet Specs\n"; //Gravitational Constant part TenToNegEleven = pow(10, -11); GravitationalConstant = (6.67 * TenToNegEleven); cout << "Gravitational Constant " << GravitationalConstant << endl; //Earth Mass Part TenToTwentyFour = pow(10, 24); EarthMass = (5.98 * TenToTwentyFour); cout << "\nEarth Mass " << EarthMass << endl; //Top Answer TopOfEscapeVelocity = (EarthMass * GravitationalConstant); TopOfEscapeVelocityX2 = (TopOfEscapeVelocity * 2); cout << "\nTop part " << TopOfEscapeVelocityX2 << endl; //Earth Radius TenToSix = pow(10, 6); EarthRadius = (6.4 * TenToSix); cout << "\nEarth Radius " << EarthRadius << endl; NonRadEscapeVelocity = (TopOfEscapeVelocityX2 / EarthRadius); cout << "\nBefore Radical " << NonRadEscapeVelocity << endl; //Final Answer EscapeVelocity = pow(NonRadEscapeVelocity, .5); cout << "\nThe Escape Velocity for Earth is " << EscapeVelocity << endl; } else if (rop == 3) { planet = 2; TenToTwentyFour = pow(10, 24); EarthMassM = (5.98 * TenToTwentyFour); cout << "\nEarth Mass " << EarthMassM << endl; rmmass = (EarthMassM/81); rmradius = (mdiameter/2); cout << "\nYou have chosen the Moon\n"; cout << "\t\tPlanet Specs\n"; //Gravitational Constant part TenToNegEleven = pow(10, -11); GravitationalConstant = (6.67 * TenToNegEleven); cout << "Gravitational Constant " << GravitationalConstant << endl; //Planet Mass cout <<"The Moon''s Mass " << rmmass << endl; //Top Answer TopOfEscapeVelocity = (rmmass * GravitationalConstant); TopOfEscapeVelocityX2 = (TopOfEscapeVelocity * 2); cout << "\nTop part " << TopOfEscapeVelocityX2 << endl; //Planet Radius cout << "The Moon''s Radius " << rmradius << endl; NonRadEscapeVelocity = (TopOfEscapeVelocityX2 / rmradius); cout << "\nBefore Radical " << NonRadEscapeVelocity << endl; //Final Answer EscapeVelocity = pow(NonRadEscapeVelocity, .5); cout << "\nThe Escape Velocity for the Moon is " << EscapeVelocity << endl; } break; I am sure that I''m probably doing something wrong considering my newness to programming (and even more newness to this whole randomness). Any help/comments would be very appreciated. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Here,you can use this code to create random numbers:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;

int main(){

srand(time(0)) ; // randomizer
// if this is not here, your rand()
// will not produce random numbers
// but instead a repeatable pattern
// time(0) is just the seed
// and since every second gives a different
// time, you''re going to get random
// numbers to work with
// if it doesn''t make sense, it doesn''t
// have to, just know that you MUST
// put srand(time(0)) in your program
// once in order to get random numbers
// when using rand()

// i want a number from 1 to 10
int maximum_desired_number = 10 ;
int my_random_number = rand() % maximum_desired_number + 1 ;
// NOTICE THE +1
// if you do not put the +1 then your random number will begin at zero
// thus if you''ll get numbers in the range of 0 to 9, not 1 to 10!!!!!
return 0 ;
}

Share this post


Link to post
Share on other sites
Thanks.
Interesting. Your way definitely is easier, and it works well, truly random.

I created a smaller program to test checking a random number (ie if (my_random_number == 1)...) so that one of the two planets would randomly be chosen. It worked, yet the modified code in my full project gives me the same result each time (in other words it wasn''t random). I suppose this must mean that something somewhere else in my code is wrong and throws of the random choseing somehow. How or what it is that is messing with my randomness I don''t know.
Sigh... more work. Thanks for your help. If and when I do solve this I''ll try to post it.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!