Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:16:00+00:00 2026-06-14T15:16:00+00:00

I need to generate 4 random numbers, each between [-45 +45] degrees. and if

  • 0

I need to generate 4 random numbers, each between [-45 +45] degrees. and if rand%2 = 0 then I want the result (the random number generated to be equal to -angle). Once the 4 random numbers are generated it is requires to scan these angles and find a lock (the point at which the angles meet). Also -3,-2,-1,… +3 in the loop in if statement indicate that the lock takes place within 6 degrees beamwidth. the code works. But can it be simplified? also The objective is to establish a lock between 2 points by scannin elevation and azimuth angles at both points.

  #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();
  }       
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:16:01+00:00Added an answer on June 14, 2026 at 3:16 pm

    Integer angles in degrees? Very questionable. Something “physical” like an angle is normally best expressed as a floating-point number, so I’d first change

    typedef double angle;
    
    struct Cscan {  // why class? This is clearly POD
      int gran; //I don't know what gran is. Perhaps this should also be floating-point.
      angle lockaz, lockel;
    };
    

    That seems to make it more difficult at first sight because neither the random-range-selection with % works anymore nor is it much use to compare floats for equality. Which is, however, a good thing, because all of this is in fact very bad practise.

    If you want to keep using rand() as the random number generator (though I’d suggest std::uniform_real_distribution), write a function to do this:

    const double pi = 3.141592653589793;  // Let's use radians internally, not degrees.
    const angle rightangle = pi/2.;      //  It's much handier for real calculations.
    
    inline angle deg2rad(angle dg) {return dg * rightangle / 90.;}
    
    angle random_in_sym_rightangle() {
      return rightangle * ( ((double) rand()) / ((double) RAND_MAX) - .5 );
    }
    

    Now you’d just do

    BS1.lockaz = random_in_sym_rightangle();
    BS1.lockel = random_in_sym_rightangle();
    BS2.lockaz = random_in_sym_rightangle();
    BS2.lockel = random_in_sym_rightangle();
    

    Then you need to do this range-checking. That’s again something to put in a dedicated function

    bool equal_in_margin(angle theta, angle phi, angle margin) {
      return (theta > phi-margin && theta < phi+margin);
    }
    

    Then you do this exhaustive search for locks. This could definitely be done more efficiently, but that’s an algorithm issue and has nothing to do with the language. Sticking to the for loops, you can still make them look much nicer by avoiding this explicit break checking. One way is good old goto, I’d propose here you just stick it in an extra function and return when you’re done

    #define TRAVERSE_SYM_RIGHTANGLE(phi) \
      for ( angle phi = -pi/4.; phi < pi/4.; phi += deg2rad(4) )
    
    int lock_k  // better give this a more descriptive name
          ( const Cscan& BS1, const Cscan& BS2, int k ) {
      TRAVERSE_SYM_RIGHTANGLE(az1) {
        TRAVERSE_SYM_RIGHTANGLE(el1) {
          TRAVERSE_SYM_RIGHTANGLE(az2) {
            TRAVERSE_SYM_RIGHTANGLE(el2) {
              if( equal_in_margin( az1, BS1.lockaz, deg2rad(6.) )
                   && equal_in_margin( el1, BS1.lockel, deg2rad(6.) )
                   && equal_in_margin( az2, BS1.lockaz, deg2rad(6.) )
                   && equal_in_margin( el2, BS2.lockel, deg2rad(6.) ) ) {
                std::cout << "locked \n" << BS1.lockaz << " " << BS1.lockel << " " << BS2.lockaz << " " << BS2.lockel << '\n'
                   << az1 << " " << el1 << " " << az2 << " " << el2 << std::endl;
                return 1;
              }
            }
          }
        }
      }
      return k;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like my question, i need to generate random numbers that have identical pairs between
A specific example I need to generate a random number between 0 and 2,
I need to generate a random port number between 2000-65000 from a shell script.
I need to create a function that will generate 2 random numbers between x
I need to be able to generate a single random number and then use
I want to generate random numbers within a range and the generated numbers should
I need to generate a lot of random numbers. I've tried using random.random but
I want a random frame.origin.x and frame.origin.y on each button click. The button need's
Is there a difference between generating multiple numbers using a single random number generator
I've generated a graph of random numbers (standard deviation 1, mean 0). I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.