C++ 随机数发生器需要帮助吗

C++ 随机数发生器需要帮助吗,c++,function,random,C++,Function,Random,我在代码中使用随机数生成器随机选择进程是否失败。我试图使用一个非常大的数字范围,使失败率非常低,但到目前为止,它一直是真的每一次。我该如何解决这个问题 //Random number generator int crash_chance(double Dis) { int chance; chance = 0; while (chance < (Dis/100), chance++){ int x = rand() % 1000000000000 + 1; //Generate

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

//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;
    }
}
**编辑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;
}
#包括
#包括
#包括
使用名称空间std;
//下面的三个功能要求用户提供所需的输入。
双高度(){
双高度;
cout>alti;
返回高度;
}
双RoC()
{
双攀援者;
攀援者;
返回攀登者;
}
双速{
双v;
coutv;
返回v;
}
//提供到达所需高度所需的时间
双倍时间(双倍A,双倍R){
双t;
t=应收;
返回t;
}
//在给定时间内水平行驶的距离
双距离行驶(双速度、双时间){
双D;
D=速度*时间;
返回D;
}
//这将把时间转换为天、小时、分钟和秒。
矢量转换时间(双输入秒){
整小时;
整数分钟;
双秒;
小时=(输入_秒/60)/60;
输入_秒-=小时*60*60;
分钟=(输入_秒/60);
输入_秒-=分钟*60;
秒=输入秒;
//将值放入向量中
向量时间(4);
次数[0]=小时;
次数[1]=分钟;
次数[2]=秒;
返回次数;
}
//以小时、分钟、秒格式打印时间。
无效打印向量(向量转换时间){

coutrand number返回整数,您的范围超出整数范围。整数为4字节,范围在-2147483648-2147483647之间

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);
}
您已经爱上了C语言的“黑暗角落”技巧

声明:

if (rand = 1){ crash = true; };
具有赋值而不是比较。它将执行的操作是将
rand
设置为
1
,然后将其用作
if
语句的基础

而且,由于
1
是真的,因此您将始终假设崩溃

正确的说法应该是:

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

但是,我仍然认为最好使用此处包含的
didcash()
函数之一,因为它可以使意图更清晰,而且犯类似错误的可能性更小。
返回值
中,而
因此它将始终精确执行一个循环。
if(rand=1)
总是正确的,这将
1
分配给
rand
。当您更改问题的本质(例如修改代码,通过将返回移动到循环外部来修复问题)时,使用
==
作为
if(rand==1)
@Cal),它打破了整个问答的概念。在这种情况下,正确的做法是问一个不同的问题,因为问题根本不同。或者,提供一个附录:我将向你展示它是如何完成的。显然,我现在非常困惑。我已经连续写了将近12个小时的代码,我是一个非常初级的程序员,我是说我不知道我在做什么。我会立即将我的代码更新为我所拥有的代码。请修复它me@CalElliott,抱歉,被叫走了。我已将您的问题简化为所需的最低限度,并更新了我的答案。建议您检查一下。我实际上设置了布尔变量,因此,如果随机数曾经=1,则会将crash设置为true,但是,如果它永远不结束=1,那么它就被设置为false(见上面的新代码)。好吧,我把我现在拥有的东西放了出来,但它仍然总是返回为true。@CalElliott。
如果(rand=1)
总是true,这会将1赋给rand。同样地,使用
=
如果(rand==1)
if (rand = 1){ crash = true; };
if (rand == 1){ crash = true; };
//       ^^
//       Comparison rather than assignment.