需要随机数生成器帮助

Random number generator help needed

本文关键字:帮助 随机数生成器      更新时间:2023-10-16

我在代码中使用随机数生成器来随机选择进程是否失败。我试图使用非常大范围的数字来使失败率非常低,但到目前为止,它每次都为真。我该如何解决这个问题?

//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;
}
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()函数之一是一个更好的主意,因为它使意图更清晰,并且犯错误的可能性更小。

rand number 返回整数,并且您的范围超出整数。整数为 4 个字节,范围介于 -2,147,483,648 - 2,147,483,647 之间