我在我的代码中使用随机数生成器来随机选择进程是否失败。我试图使用非常大的数字来使故障率非常低,但到目前为止它每次都会保持真实。我该如何解决?
//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;
}
}
修改
即使我修复了该代码以将返回移到循环之外,它仍然表示崩溃。
我会按要求添加调用该函数的代码。
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;
}
答案 0 :(得分:2)
int x = rand() % 1000000000000 + 1;
return x;
该片段中绝大多数数字都不为零,因此被认为是真的。实际上,可能所有它们,假设您正在添加一个而且几乎肯定溢出的大数字会阻止回绕为零。
如果您想根据百分比输入返回指示崩溃的真值,您可以使用以下内容:
int didCrash (int chanceOfCrash) {
return ((rand() % 101) < chanceOfCrash);
}
它不是完全分发,但应该足以满足您的目的。
并且,如果整体故障率不够好,您可以通过以下方式调整它以获得更高的分辨率:
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.
v /= 10000;
return (val < chanceOfCrash);
}
这使您可以将分辨率指定为0.0001
,以实现非常精细的碰撞控制。
关于您添加主叫代码的编辑:
rand = crash_chance(D);
bool crash;
if (rand = 1){ crash = true; };
if (rand != 1){ crash = false; };
你已经陷入了C语言的“黑暗角落”伎俩。
声明:
if (rand = 1){ crash = true; };
有分配而不是比较。它的作用是将rand
设置为1
,然后将其用作if
语句的基础。
而且,由于1
为真,您总是假设崩溃。
使用的正确陈述是:
if (rand == 1){ crash = true; };
// ^^
// Comparison rather than assignment.
然而,我仍然认为使用此处包含的didCrash()
函数之一是更好的主意,因为它使意图更清晰,并且犯错误的可能性更小。< / p>
答案 1 :(得分:0)
rand number返回整数,你的范围超出整数。整数为4个字节,范围介于-2,147,483,648 - 2,147,483,647
之间