How to generate 100 <x,y> random points on a circle and display them in C

1 Answer

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

// 10 >= sqrt(x**2 + y**2) <= 15

int _random(int n) {
	int rand_max = RAND_MAX - (RAND_MAX % n);
	int rnd;
	while ((rnd = rand()) > rand_max);
	
	return rnd / (rand_max / n);
}

int main()
{
	unsigned long arr[31] = {0}; 

	srand(time(NULL));
	
	for (int i = 0; i < 100; ) {
		int x = _random(31) - 15;
		int y = _random(31) - 15;
		int border = x * x + y * y;
		if (border >= 100 && border <= 225) {
			arr[15 + y] |= 1 << (x + 15);
			i++;
		}
	}

	for (int i = 0; i < 31; i++) {
		for (int j = 0; j < 31; j++)
			printf((arr[i] & 1 << j) ? ". " : "  ");
		printf("\n");
	}

	return 0;
}



/*
run:

                          .     .   . . .                     
                  . .   .       .   . .     .                 
                                      .   .   . .             
          .       .           .                 .             
        .       . .         .     .           .   .           
                  .                                           
                                                    . .       
            . .                                 .   . .       
        . .   .                               .     .         
        .                                                     
                                                          .   
                                                  .           
                                                    . .       
      . .                                             . .     
      .                                                     . 
    .                                               .         
          .                                           .   .   
    .   .                                           .   .     
    . .                                                   .   
                                                      .       
    .                                                 .       
      . . .                                           .       
                  .                       .     .             
          .   .                                       .       
              . .               .     . .         .           
            .   .           . .                               
            .               .   .             .               
                  . .                                         
                          .                                   
                                                              
*/

 



answered Aug 21, 2023 by avibootz
...