0
void *thread(void *param) {

    float distancia;
    int numeroPontos = atoi(param);
    int i;
    float numeroAleatorio_x;
    float numeroAleatorio_y;

    for (i = 0 ; i < numeroPontos; i++) {

        x = rand() % 1000;
        y = rand() % 1000;
    }  

    pthread_exit(0);
}

So far I've got this, it can only generate positive numbers. How do I generate negative ones?

Thanks in advance

  • Shift the range. Generate (0,2*x) then transform to (-x,x) by subtracting x. – user2864740 Jun 14 '20 at 18:24
  • 2
    If `x` is in `(0, 1)`, then `2 * x - 1` is in `(-1, 1)`. – bereal Jun 14 '20 at 18:26
  • 2
    Here is specified how to generate randon numbers within a range. https://stackoverflow.com/questions/1202687/how-do-i-get-a-specific-range-of-numbers-from-rand – Josep Jun 14 '20 at 18:27
  • 1
    An other post with information on how to generate random numbers: https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c – Yusef Maali Jun 14 '20 at 18:29
  • It is generating negative numbers now with Josep suggestion, but only integer ones how do I generate decimal ones? – SoapTastesOk Jun 14 '20 at 18:36
  • Generate integer random numbers between -1000 and 1000, then divide by `1000.0`, to get floating point numbers from -1 to 1. – user3386109 Jun 14 '20 at 19:22
  • 1
    Related: https://stackoverflow.com/questions/58420853/how-to-generate-a-n-sized-random-float-array-that-sums-up-to-0-0/58422276#58422276 – Roberto Caboni Jun 14 '20 at 19:54
  • `int x = rand() % 1000 - 500;` or `double x = (rand() % 1000 - 500)/500.0;` – 0___________ Jun 14 '20 at 19:59
  • for the -1000 to 1000 `int x = rand() % 2000 - 1000;` simple math – 0___________ Jun 14 '20 at 20:00
  • `double x = 2.0 * rand() / RAND_MAX - 1.0` – Weather Vane Jun 14 '20 at 20:54
  • Does this answer your question? [How to set rand() result to values in a certain range including negatives?](https://stackoverflow.com/questions/16410136/how-to-set-rand-result-to-values-in-a-certain-range-including-negatives) – Jongware Jun 16 '20 at 11:20

0 Answers0