【问题标题】:Random number generator help needed需要随机数生成器帮助
【发布时间】: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,对不起,被叫走了。我已将您的问题简化为所需的最低限度并更新了我的答案。建议你去看看。

标签: c++ function random


【解决方案1】:
int x = rand() % 1000000000000 + 1;
return x;

该 sn-p 中的绝大多数数字将非零,因此被认为是正确的。事实上,可能所有个,因为您要添加一个并且您几乎肯定会溢出的大数字会阻止回绕为零。

如果你想返回一个表示崩溃的真值,基于百分比输入,你可以使用类似的东西:

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

【讨论】:

  • 我实际上设置了布尔变量,所以如果随机数 = 1,它会将 crash 设置为 true,但如果它永远不会结束 = 1,则将其设置为 false(参见上面的新代码)。跨度>
  • 好吧,我把我现在所拥有的,但它仍然总是回归真实。
  • @CalElliott。 if (rand = 1) 始终为真,这将 1 分配给 rand。使用== 这样if (rand == 1)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2015-06-27
    • 1970-01-01
    • 2014-11-30
    相关资源
    最近更新 更多