Generating random numbers

Our C++ professor gave us a code to generate random numbers, but when I enter the code and run it nothing happens. Here is the code I have:


#include <windows.h>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

vector<int> example;
vector<char> letters;

unsigned seed;

int number, temp, start_pos, vec_length, ctr;
bool posi_change;
char temp_chr;


int main()
{
seed = time(0);
srand(seed);

for (int x = 1; x <= 50, x++;)
{
number = rand() % (100 + 1);
}
if (number > 64 && number < 91)
{
cout << number << '\n';
example.push_back(number);
letters.push_back(static_cast < char > (number));

}
if (number > 47 && number < 58)
{
cout << number << '\n';
example.push_back(number);
letters.push_back(static_cast<char>(number));
}
if (number > 96 && number < 123)
{
cout << number << '\n';
example.push_back(number);
letters.push_back(static_cast<char>(number));
}
cout << '\n\n';
cout << example.size() << '\n';
cout << '\n\n';

system("pause");

for (int y = 0; y < example.size(); y++)
{
for (int z = 0; z < example.size() - 1; z++)
{
if (example[2]<example[2 + 1])
{
temp = example[2];
example[2] = example[2 + 1];
example[2 + 1] = temp;
}
}

}
for (int z = 0; z < example.size(); z++)
{
cout << example[z] << '\n';

start_pos = 0;
vec_length = letters.size();
posi_change = true;

while (posi_change&& start_pos <= letters.size())
{
posi_change = false;
ctr = start_pos + 1;

for (ctr; ctr < vec_length; ctr++)
{
if (letters[start_pos] < letters[ctr])
{
temp_chr = letters[start_pos];
letters[start_pos] = letters[ctr];
letters[ctr] = temp_chr;

posi_change = true;
}
}
start_pos = start_pos + 1;
}
}
for (int w = 0; w < letters.size(); w++)
{
cout << letters[w] << " " << static_cast<int>
(letters[w]) << '\n';
}
system("pause");

return 0;
}

closed account (E0p9LyTq)
A couple of things stand out:

1. PLEASE learn to use code tags, it makes your code easier to read, and to comment.
You can go back, edit your post and add code tags.
http://www.cplusplus.com/articles/jEywvCM9/

2. Your first for loop has a logic error, it should be:
for (int x = 1; x <= 50; x++)

3. All the if statements after the first for loop run only once, so your vector only in very exceptional cases get even one number pushed back. Maybe they should be within the for loop after the random number is generated?

4. Global variables are not the best design, and having all your variables being declared as global can cause problems. Especially when you use functions.

5. Using the C library pseudo-random generator when writing C++ code. *SMH* Even the C ISO Standard recommends not using srand and rand.

C++ has a robust random number library in the header <random>.
The problem is essentially to check if a random number falls within any of the non-overlapping ranges:
Random number generation - apart from the use of rand() the other mistake is that you're generating random numbers in the range 1-100 but want to check if the generated number falls within the range 97-122 (a-z) most of which is never going to happen
Non-overlapping ranges check - you don't need to check each range separately, rather you can create std::set<std::pair<int,int>> with the lower and upper bounds of each range and a custom comparator and check if you can std::set<std::pair<int,int>>.find the number within the set

The following program does both:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <set>
#include <chrono>

using Range = std::pair<int, int>;
struct RangeCompare//custom comparator
{
    bool operator()(const Range& lhs, const Range& rhs) const
    {
        return lhs.second < rhs.first;
    }
};
bool in_range(const std::set<Range, RangeCompare>& ranges, int value)
{
    return ranges.find(Range(value, value)) != ranges.end();
}
int main()
{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine dre(seed);
    // use engine to generate integral numbers between 0 and 127 (both included)
    std::uniform_int_distribution<int> di(0,127);

    std::vector<int> numbers;
    for (int i = 0; i < 50; ++i)numbers.push_back(di(dre));

    std::set<Range, RangeCompare> ranges;
    ranges.insert(Range(48, 57));
    ranges.insert(Range(65, 90));
    ranges.insert(Range(97, 122));

    std::vector<char> letters;
    for (const auto& elem : numbers)
    {
        if(in_range(ranges, elem))
        {
            letters.push_back(static_cast < char > (elem));

        }
    }
    for (const auto& elem : letters)
    {
        std::cout << elem << " ";
    }
    std::cout << '\n';
}
Last edited on
Topic archived. No new replies allowed.