【发布时间】:2015-01-25 17:39:15
【问题描述】:
我在我的代码中使用随机数生成器来随机选择进程是否失败。我正在尝试使用很大范围的数字来使失败率非常低,但到目前为止,它每次都保持正确。我该如何解决?
//Random number generator
int crash_chance(double Dis) {
int chance;
chance = 0;
while (chance < (Dis/100), chance++){
int x = rand() % 1000000000000 + 1; //Generate an integer between 1 and 1000000000000
return x;
}
}
编辑:
即使我修复了该代码以将 return 移到循环之外,它仍然表示崩溃。
我会按要求添加调用该函数的代码。
rand = crash_chance(D);
bool crash;
if (rand = 1){ crash = true; };
if (rand != 1){ crash = false; };
**编辑 2 ** 所以下面的代码无法修复?
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
//the three functions below ask the user for the required input.
double altitude(){
double alti;
cout << "Please input the 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 your current speed over ground in m/s" << endl;
cin >> v;
return v;
}
// Gives you the time it will take to reach desired altitude
double time(double A, double R){
double t;
t = A / R;
return t;
}
//Distance travelled horizontally in given time
double distancetravelled(double Veloc, double Time){
double D;
D = Veloc*Time;
return D;
}
//This will convert time to days, hours, minutes, and 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 into a vector
vector<double>times(4);
times[0] = hours;
times[1] = minutes;
times[2] = seconds;
return times;
}
//prints the time in hours,minutes,seconds format.
void print_vector(vector<double>converted_time){
cout << "The time it will take for the plane to reach its desired altitude is: " << endl;
cout << converted_time[0] << " hours, ";
cout << converted_time[1] << " minutes and ";
cout << converted_time[2] << " seconds" << endl;
cout << endl;
}
// This prints the distance over ground travelled and if there was a malfuntion.
void print_result (double V, double D){
// This is for the distance it will travel horizontally in the time it takes to to climb.
cout << "The distance over ground you will travel will be ";
cout << D << " meters, or "<< (D/1000)<< "Km" <<endl;
cout << endl;
}
//This prints the angle and also figures out if the plane should be angled up or down.
void print_angle(double Th, double Alt, bool C){
if (Alt < 0){ cout << "The angle below the horizontal the plane should be pointed is " << Th << " degrees." << endl;
cout << endl;
}
else if (Alt > 0){ cout << "The angle above the horizontal the plane should be pointed is " << Th << " degrees."<< endl;
cout << endl;
}
//This will determine if the angle was safe or not.
if (Th > 60){
cout << "The angle required to reach this altitude with the specified climb rate" << endl;
cout << "was too great, the pilot attempted the climb and stalled the plane" << endl;
cout << "resulting in a crash" << endl;
cout << endl;
}
if (C == true){
cout << "EMERGENCY! The plane experienced serious problems while ascending," << endl;
cout << " the pilot has lost control and has crashed!" << endl;
cout << endl;
if (C == false){ cout << " No problems were experienced while ascending" << endl; }
}
}
//This will get the angle required for the plane to point its 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 to get 0-1,000,000
double val = 0.0;
for (int i = 0; i < 100; i++){
val += (double)((rand() % 10001));
}
// Divide by 10,000 to get 0.0000-100.0000
// and 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();
A = 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: the print results do not print ONLY what their names are. this is meerly the first thing they print.
print_result(V, D);
print_vector(foo);
print_angle(Theta, A, crash);
return 0;
}
【问题讨论】:
-
return在你的while内,所以它总是会精确地执行一个循环。 -
if (rand = 1)始终为真,这会将1分配给rand。使用==这样if (rand == 1) -
@Cal,当您更改问题的本质时(例如修改代码以通过将返回移到循环之外来解决您的问题),它会破坏整个问答概念。在这种情况下,正确的做法是提出一个不同的问题,因为问题根本不同。或者,提供一个附录:我会告诉你它是如何完成的。
-
显然我现在很困惑。我在这段代码上工作了将近 12 个小时,而且我是一个非常初级的程序员,我真的不知道我在做什么。我会将我的代码更新为我现在所拥有的。请为我修好它
-
@CalElliott,对不起,被叫走了。我已将您的问题简化为所需的最低限度并更新了我的答案。建议你去看看。