vendredi 24 mars 2017

drand48() isn't working in my c program

for some reason neither of my drand48() are returning random numbers between 0-1 like they're supposed to... x was resulting in only the inputted number that is n, and y stayed 0

/// Challenge: Darts
/// circle of radius 0.5 inside a 2x2 square
/// throw darts (x) times and calculate random hits

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

int main() {
  double n, t, z, a;
  int x, y;

  printf("How many darts do you want to throw? ");
  scanf("%lf", &n);
  printf("Okay, let's throw %lf darts\n. . .\n", n);

  z = 0;
  a = 0;
  t = 0;

  while (t <= n) {
    x = drand48(); /// returns # 0-1
    x = x - 0.5; /// puts it on a graph with domain/range -0.5 - 0.5
    y = drand48();
    y = y - 0.5;

    if ((((x^2) + (y^2))^2) <= 0.25) {
      z = z + 1;
    }
    t = t + 1;
  }
  a = z / (t - 1);
  printf("%lf\n", a);
}

/// remember to compile with gcc darts.c -lm
/// remember to run with ./a.out




Aucun commentaire:

Enregistrer un commentaire