c++ - Random number generator help needed -


i using random number generator in code randomly select if process fails or not. trying use large range of numbers make fail rate low far keeps coming true every time. how fix this?

//random number generator int crash_chance(double dis) { int chance; chance = 0; while (chance < (dis/100), chance++){      int x = rand() % 1000000000000 + 1; //generate integer between 1 , 1000000000000     return x;     } } 

edit:

even when fix code move return outside of loop, still indicates crash.

i'll add code calls function, requested.

rand = crash_chance(d); bool crash; if (rand = 1){ crash = true; }; if (rand != 1){ crash = false; }; 

**edit 2 ** code below cannot fixed?

#include<iostream> #include<cmath> #include<vector> using namespace std;  //the 3 functions below ask user required input. double altitude(){ double alti; cout << "please input change in altitude in meters:"; cin >> alti; return alti; }  double roc() { double climbr; cout << "please input climb rate in m/s:"; cin >> climbr;   return climbr; }  double speed(){ double v; cout << "please input current speed on ground in m/s" << endl; cin >> v; return v; }  //  gives time take reach desired altitude double time(double a, double r){ double t; t = / r; return t; }  //distance travelled horizontally in given time double distancetravelled(double veloc, double time){  double d; d = veloc*time;  return d; }   //this convert time days, hours, minutes, , seconds. vector<double> converted_time(double input_seconds){ int hours; int minutes; double seconds; hours = (input_seconds / 60) / 60; input_seconds -= hours * 60 * 60; minutes = (input_seconds / 60); input_seconds -= minutes * 60; seconds = input_seconds; //puts values vector vector<double>times(4); times[0] = hours; times[1] = minutes; times[2] = seconds;  return times;   }  //prints time in hours,minutes,seconds format. void print_vector(vector<double>converted_time){  cout << "the time take plane reach desired altitude is: " << endl; cout << converted_time[0] << " hours, "; cout << converted_time[1] << " minutes , "; cout << converted_time[2] << " seconds" << endl; cout << endl; }   // prints distance on ground travelled , if there malfuntion. void print_result (double v, double d){  // distance travel horizontally in time takes to climb. cout << "the distance on ground travel "; cout << d << " meters, or "<< (d/1000)<< "km" <<endl; cout << endl; }  //this prints angle , figures out if plane should angled or down. void print_angle(double th, double alt, bool c){ if (alt < 0){ cout << "the angle below horizontal plane should pointed " << th << "    degrees." << endl; cout << endl; } else if (alt > 0){ cout << "the angle above horizontal plane should pointed " << th   << " degrees."<< endl; cout << endl; } //this determine if angle safe or not. if (th > 60){     cout << "the angle required reach altitude specified climb rate" << endl;     cout << "was great, pilot attempted climb , stalled plane" << endl;     cout << "resulting in crash" << endl;     cout << endl; } if (c == true){     cout << "emergency! plane experienced serious problems while ascending," << endl;     cout << " pilot has lost control , has crashed!" << endl;     cout << endl;     if (c == false){ cout << " no problems experienced while ascending" << endl; } }  }  //this angle required plane point nose above horizontal.  double get_angle(double alt, double dis){ double angle_degrees; double angle = atan(alt / dis); angle_degrees = angle*(180 / 3.14159); return angle_degrees; }   //random number generator int didcrash(double chanceofcrash) {     // add 0-10,000 in 100 loops 0-1,000,000      double val = 0.0;     (int = 0; < 100; i++){         val += (double)((rand() % 10001));     }          // divide 10,000 0.0000-100.0000         //  , decide whether crashing or not.          val /= 10000;      return (val < chanceofcrash); }       // function starts here. int main(){ double a; double r; double t; double v; double d; double theta; int rand; r = roc(); = altitude(); t = time(a, r); vector<double> foo = converted_time(t);  double hours = foo[0];  double minutes = foo[1]; double seconds = foo[2];    v = speed(); d = distancetravelled(t,v);  rand = didcrash(d); bool crash; if (rand == 1){ crash = true; }; if (rand != 1){ crash = false; };  theta = get_angle(a, d); //note: print results not print names are. meerly first thing   print. print_result(v, d); print_vector(foo); print_angle(theta, a, crash);        return 0; } 

int x = rand() % 1000000000000 + 1; return x; 

the vast majority of numbers out of snippet non-zero, hence considered true. in fact, possibly all of them, given you're adding 1 , almost-certainly-overflowing large number prevent wraparound zero.

if want return truth value indicating crash, based on percentage input, can use like:

int didcrash (int chanceofcrash) {     return ((rand() % 101) < chanceofcrash); } 

it's not perfectly distributed should enough purposes.

and, if integral failure rate not enough, adapt more resolution like:

int didcrash (double chanceofcrash) {     // add 0-10,000 in 100 loops 0-1,000,000      double val = 0.0;     (int = 0; < 100; i++)         val += (double)((rand() % 10001)      // divide 10,000 0.0000-100.0000     //  , decide whether crashing or not.      v /= 10000;      return (val < chanceofcrash); } 

this allows specify resolution down 0.0001 fine crash control.


with regard edit added calling code:

rand = crash_chance(d); bool crash; if (rand = 1){ crash = true; }; if (rand != 1){ crash = false; }; 

you have fallen "dark corner" trick of c language.

the statement:

if (rand = 1){ crash = true; }; 

has assignment rather comparison in it. set rand 1 use basis of if statement.

and, since 1 true, always assume crash.

the correct statement use have been:

if (rand == 1){ crash = true; }; //       ^^ //       comparison rather assignment. 

however, still think it's better idea use 1 of didcrash() functions contained here since makes intent clearer , there's less chance of making mistake loke this.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -