How to create 10 random points in C++

2 Answers

0 votes
#include <iostream>
#include <random>
#include <vector>

struct Point {
    double x;
    double y;
};

// Function to generate one random point
Point generatePoint(std::mt19937& gen, std::uniform_real_distribution<>& dist) {
    return Point{ dist(gen), dist(gen) };
}

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dist(-10.0, 100.0);

    std::vector<Point> points;

    for (int i = 0; i < 10; ++i) {
        points.push_back(generatePoint(gen, dist));
    }

    for (int i = 0; i < points.size(); i++) {
        std::cout << "Point " << i + 1 << ": ("
                  << points[i].x << ", " << points[i].y << ")\n";
    }
}



/*
run:

Point 1: (60.7382, 36.5041)
Point 2: (94.5546, 41.4542)
Point 3: (41.1237, 20.757)
Point 4: (61.5101, 44.8168)
Point 5: (93.1159, 97.1005)
Point 6: (4.56475, -7.34092)
Point 7: (8.40699, 23.4775)
Point 8: (10.5186, 1.63058)
Point 9: (49.2773, 34.5822)
Point 10: (24.5703, 27.7971)

*/

 



answered Jun 5 by avibootz
edited Jun 5 by avibootz
0 votes
#include <iostream>
#include <random>
#include <vector>

struct Point {
    int x;
    int y;
};

// Generate one random point in [10, 90]
Point generatePoint(std::mt19937& gen, std::uniform_int_distribution<int>& dist) {
    return Point{ dist(gen), dist(gen) };
}

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(10, 90);

    std::vector<Point> points;

    for (int i = 0; i < 10; ++i) {
        points.push_back(generatePoint(gen, dist));
    }

    for (int i = 0; i < points.size(); i++) {
        std::cout << "Point " << i + 1 << ": ("
                  << points[i].x << ", " << points[i].y << ")\n";
    }
}



/*
run:

Point 1: (54, 25)
Point 2: (26, 40)
Point 3: (47, 41)
Point 4: (20, 33)
Point 5: (72, 36)
Point 6: (10, 85)
Point 7: (80, 65)
Point 8: (80, 82)
Point 9: (44, 43)
Point 10: (22, 81)

*/

 



answered Jun 5 by avibootz
...