gpt4 book ai didi

c++ - rand() 函数 C++

转载 作者:行者123 更新时间:2023-11-28 00:52:18 25 4
gpt4 key购买 nike

我需要生成 4 个随机数,每个随机数在 [-45 +45] 度之间。如果 rand%2 = 0 那么我想要结果(生成的随机数等于 -angle)。一旦生成了 4 个随机数,就需要扫描这些角度并找到一个锁(角度相交的点)。此外,if 语句中循环中的 -3、-2、-1、... +3 表示锁定发生在 6 度波束宽度内。代码有效。但是可以简化吗?目标也是通过扫描两点的仰角和方位角在两点之间建立锁定。

  #include <iostream>
#include <conio.h>
#include <time.h>
using namespace std;

class Cscan
{
public:
int gran, lockaz, lockel;

};

int main()
{
srand (time(NULL));
int az1, az2, el1, el2, j, k;


BS1.lockaz = rand() % 46;
BS1.lockel = rand() % 46;
BS2.lockaz = rand() % 46;
BS2.lockel = rand() % 46;

k = rand() % 2;
if(k == 0)
k = -1;
BS1.lockaz = k*BS1.lockaz;

k = rand() % 2;
if(k == 0)
k = -1;
BS1.lockel = k*BS1.lockel;

k = rand() % 2;
if(k == 0)
k = -1;
BS2.lockaz = k*BS2.lockaz;

k = rand() % 2;
if(k == 0)
k = -1;
BS2.lockel = k*BS2.lockel;

for(az1=-45; az1<=45; az1=az1+4)
{
for(el1=-45; el1<=45; el1=el1+4)
{
for(az2=-45; az2<=45; az2=az2+4)
{
for(el2=-45; el2<=45; el2=el2+4)
{

if((az1==BS1.lockaz-3||az1==BS1.lockaz-2||az1==BS1.lockaz-1||az1==BS1.lockaz||az1==BS1.lockaz+1||az1==BS1.lockaz+2||az1==BS1.lockaz+3)&&

(az2==BS2.lockaz-3||az2==BS2.lockaz-2||az2==BS2.lockaz-1||az2==BS2.lockaz||az2==BS2.lockaz+1||az2==BS2.lockaz+2||az2==BS2.lockaz+3)&&

(el1==BS1.lockel-3||el1==BS1.lockel-2||el1==BS1.lockel-1||el1==BS1.lockel||el1==BS1.lockel+1||el1==BS1.lockel+2||el1==BS1.lockel+3)&&

(el2==BS2.lockel-3||el2==BS2.lockel-2||el2==BS2.lockel-1||el2==BS2.lockel||el2==BS2.lockel+1||el2==BS2.lockel+2||el2==BS2.lockel+3))
{
cout << "locked \n" << BS1.lockaz << " " << BS1.lockel << " " << BS2.lockaz << " " << BS2.lockel <<endl
< az1 << " " << el1 << " " << az2 << " " << el2 << endl;
k = 1;
break;
}
if(k==1)
break;
}
if(k==1)
break;
}
if(k==1)
break;
}
if(k==1)
break;
}
_getch();
}

最佳答案

BS1.lockaz = rand() % 91 - 45;
BS1.lockel = rand() % 91 - 45;
BS2.lockaz = rand() % 91 - 45;
BS2.lockel = rand() % 91 - 45;

关于c++ - rand() 函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13471241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com