Hello everyone

I have a task to generate 300 random particles in a 2-D plane ( 1 x 1)

The steps are given as :
1. Generate two numbers
2. Scale them to max dimension
3. Repeat 1 and 2 in a 'for loop' to generate 300 particles ans store them in a verctor px and py .
4. get the output

Ok I just created this code but I am confused at some point and need help

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

void main()
{
clrscr();
int a[20][20]; //array for storing ouput numbers
int px,py;    // x and y axis
for (px=0; px<20; px++)    //x axis with the range of 20
{
 for(py=0; py<20; py++)    //y axis with the range of 20
 {
  a[px][py]=rand() %1000 + 1;  //Random particles b/w 1 to 1000
  printf("a[%d][%d] = %d\n", px,py, a[px][py]);   //axis points and particles value
 }
}
getch();
}

This code prints the random numbers between 1 to 1000 for the range of 20 x 20 and that will be 400 random numbers , I want to print the 300 random numbers under the area of 1 x 1 meter , so what could be the possible solution ? where I a going wrong

You're conflating "random particles" with "random numbers". Since this is a 2D plane, each particle would presumably have a random X and Y coordinate:

#include <stdio.h>
#include <stdlib.h>

struct pairtickle { int x, y; };

/**
    Generates a random particle given the upper bound
    of each coordinate. The lower bound is always 0.
*/
struct pairtickle random_particle(int x_max, int y_max)
{
    struct pairtickle result;

    result.x = rand() % x_max;
    result.y = rand() % y_max;

    return result;
}

int main(void)
{
    struct pairtickle particles[300];
    int i;

    /* Generate the 300 random particles */
    for (i = 0; i < 300; i++) {
        particles[i] = random_particle(20, 20);
    }

    /* Display them */
    for (i = 0; i < 300; i++) {
        printf("(%d,%d)\n", particles[i].x, particles[i].y);
    }

    return 0;
}

Edit:
Sorry didn't saw ur coding and That's so nice of you that you guided me in a right direction

Edited 3 Years Ago by Farhan_4

The X,Y coordinates for each particle on your 2D plane is the random location. If all you want to do is to output the random location for each particle, then my example program is sufficient.

Comments
pairtickle - my pairtickle physicist wife will chuckle at that one! :-)
This article has been dead for over six months. Start a new discussion instead.