Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to create 10 random points in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
    double x;
    double y;
} Point;

// Function to generate one random point in range [10, 100]
Point generatePoint() {
    Point p;
    p.x = 10 + (rand() / (double)RAND_MAX) * 90;  // 10 to 100
    p.y = 10 + (rand() / (double)RAND_MAX) * 90;  // 10 to 100
    
    return p;
}

int main() {
    srand(time(NULL));  // Seed RNG

    Point points[10];

    for (int i = 0; i < 10; i++) {
        points[i] = generatePoint();
    }

    for (int i = 0; i < 10; i++) {
        printf("Point %d: (%.2f, %.2f)\n", i + 1, points[i].x, points[i].y);
    }

    return 0;
}


/*
run:

Point 1: (85.63, 42.87)
Point 2: (39.93, 32.93)
Point 3: (18.59, 66.26)
Point 4: (55.00, 46.15)
Point 5: (14.73, 80.81)
Point 6: (97.21, 30.76)
Point 7: (56.14, 39.67)
Point 8: (17.52, 26.53)
Point 9: (31.14, 11.34)
Point 10: (50.86, 99.92)

*/

 



answered Jun 5 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
    int x;
    int y;
} Point;

// Generate one random point in [10, 90]
Point generatePoint() {
    Point p;
    p.x = rand() % 81 + 10;  // 10–90
    p.y = rand() % 81 + 10;  // 10–90
    return p;
}

int main() {
    srand(time(NULL));  // Seed RNG

    Point points[10];

    for (int i = 0; i < 10; i++) {
        points[i] = generatePoint();
    }

    for (int i = 0; i < 10; i++) {
        printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);
    }

    return 0;
}



/*
run:

Point 1: (49, 66)
Point 2: (47, 52)
Point 3: (80, 57)
Point 4: (62, 36)
Point 5: (66, 10)
Point 6: (40, 62)
Point 7: (64, 22)
Point 8: (16, 82)
Point 9: (89, 63)
Point 10: (56, 37)

*/

 



answered Jun 5 by avibootz

Related questions

...