Home c++ Visual C++: Generation of random numbers (Rand)

Visual C++: Generation of random numbers (Rand)

Author

Date

Category

How to generate numbers from the interval from 256 to 2048 in increments 64?


Answer 1, Authority 100%

int n = (rand ()% 28 + 4) * 64;

Answer 2, Authority 50%

Probably so, the number is generated in the function and is displayed, and in the main function generates 100 numbers to check.

# include & lt; Cstdlib & gt;
#Include & lt; iostream & gt;
Size_t GenerateRandNumber ()
{
  Size_T RANDNUMBER = (((Rand ()% 28) + 4) * 64);
  STD :: COUT & LT; & LT; "RAND X64 Number:" & lt; & lt; RANDNUMBER & LT; & LT; "\ n";
  RETURN RANDNUMBER;
}
Int Main (int argc, char * argv [])
{
  For (Size_t i = 0; i & lt; 100; i ++)
  {
    Size_t Number = GenerateRandNumber ();
    If (Number & LT; 256 || Number & gt; 2048 || (Number% 64)! = 0)
    {
      STD :: COUT & LT; & LT; "Generate Error Number! \ N";
    }
  }
  SYSTEM ("PAUSE");
  Return 0;
}

hah yet wrote 3 answers already)


Answer 3, Authority 25%

something like this:

# include & lt; iostream & gt;
#Include & lt; Ctime & gt;
#Include & lt; Cstdlib & gt;
Using Namespace STD;
INT MAIN ()
{
  SRAND ((unsigned) TIME (0));
  INT RANDOM_INTEGER;
  INT STEP = 64; //Step
  INT LOWEST = 256 / STEP, HIGHEST = 2048 / STEP; // Left right border.
  INT RANGE = (HIGHEST-LOWEST) +1;
  For (int index = 0; index & lt; 20; index ++) {// index - how many people you need.
    RANDOM_INTEGER = LOWEST + INT (RANGE * RAND () / (RAND_MAX + 1.0));
    COUT & LT; & LT; RANDOM_INTEGER * STEP & LT; & LT; Endl;
  }
}

Answer 4, Authority 12%

Good day!
With the use of the new C++ 11x standard, you can write like this:

# include & lt; random & gt;
#Include & lt; iostream & gt;
Int Main (int argc, char * argv [])
{
  Size_t Step = 64; // Step
  Size_t Lowest = 256 / Step, Highest = 2048 / Step; // Left and Right Border
  Size_t n = 100; // Required number of numbers
  STD :: Random_Device RD;
  STD :: MT19937 GEN (RD ());
  STD :: uniform_int_distribution & lt; & gt; DIS (Lowest, Highest);
  While (N--) {
    // Generating the next number
    int x = dis (gen) * 64;
    // Checking the correctness of generation
    if (x & lt; 256 || x & gt; 2048)
      STD :: COUT & LT; & LT; "Error:" & lt; & lt; x & lt; & lt; "OUTSIDE OF RANGE" & LT; & LT; STD :: ENDL;
  }
  Return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions