1

How to generate random float numbers within a range (not from 0) in C?

I tried the code mentioned at this link : How to generate a random number from within a range . But this one always returns a "1" when I pass in maximum - minimum +1.

Thanks.

Community
  • 1
  • 1
priti
  • 35
  • 1
  • 5
  • Can you show the code you tried? There are numerous answers in that question, some better than others, and it's possible you have a bug causing the issue. You should also specify what you mean by "random float numbers". Does that mean if you ask for a number between 5 and 10 that 5.479234 is good example, or do you want whole numbers only? – Retired Ninja Dec 11 '14 at 08:25
  • 3
    What did you try? The given link is for integer random numbers, not float – phuclv Dec 11 '14 at 08:25

1 Answers1

3

Try this:

#include <time.h>
#include <stdlib.h>

float GetRand(float min,float max)
{
    static int firstTime = 1;
    if (firstTime == 1)
    {
        firstTime = 0;
        srand((unsigned int)time(NULL));
    }
    return (max-min)*rand()/RAND_MAX+min;
}

Please note that this function is not thread-safe, so you may want to call srand beforehand.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • @JoachimPileborg: 1. That's not true (you can try by running the code). 2. Why does it matter if the result is a fraction or not? It is randomly a fraction or a whole number (probably fraction in most cases), but what difference does it make as far as the question goes? – barak manos Dec 11 '14 at 08:39
  • @JoachimPileborg: `max` and `min` are both `float`, hence `max-min` is `float`, and hence the result of the division operation is a `float`. C does not distinguish between "fractions" and "whole numbers", but between types (`float` and `int` in this case)!!! – barak manos Dec 11 '14 at 08:44
  • @JoachimPileborg: No they won't. Please run the code on your favorite IDE. – barak manos Dec 11 '14 at 08:44
  • @KlasLindbäck Yes, apparently I am... I think I need a coke! – Some programmer dude Dec 11 '14 at 08:49
  • @KlasLindbäck: I think it's more about not understanding the concept of types, and ascribing `float` to "fractions" and `int` to "non-fractions". – barak manos Dec 11 '14 at 08:49
  • @KlasLindbäck: Haha, good point! It makes very little sense that this user could actually make such wrong conclusions (unless his entire SO history is on non-type-safe languages, which although I did not look into it, I doubt is the case here). So I stand corrected, it must have been the lack of coke or something :) In any case, it feels good to know that we all make mistakes sometimes (even 100K+ users). – barak manos Dec 11 '14 at 09:22
  • Oh, I make a *lot* of mistakes, all the time! :) But not always (I hope) as grave as this one. :) – Some programmer dude Dec 11 '14 at 09:33
  • @JoachimPileborg: It's a good thing I didn't check your profile beforehand, otherwise I would have assumed that my answer was definitely wrong somewhere :) – barak manos Dec 11 '14 at 09:39
  • A weakness with this approach lies in the number of potential different results is `RAND_MAX + 1` which could be as small as 32768 different answers. This is likely sufficient for OP's goals, but a general solution would extract more than 15 random bits to form the `float`. – chux - Reinstate Monica Dec 11 '14 at 13:49
  • @chux: Thanks. In addition, the values of `min` and `max` by themselves, as well as the difference between them (i.e., the range) are also limited here. However, OP did not specify any requirements besides computing a random `float` value, so I gave the simplest solution that came to mind. – barak manos Dec 11 '14 at 13:51
  • @chux:Thanks for your suggestions as well.This is sufficient for me as of now.I will look into the details you had mentioned once I get rest of my code working. – priti Dec 11 '14 at 17:16