Link to home
Start Free TrialLog in
Avatar of Chrysaor
Chrysaor

asked on

Random numbers from a specific range in C++

I want a function which will take random numbers from a range like this: from 2 to 120, excluding integers x and y and so on..

Thanks a lot..
SOLUTION
Avatar of sistemu
sistemu
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Infinity08

#include <cstdlib>
#include <ctime>
 
 
// calculate a random value between min and max (inclusive)
int getRand(int min, int max) {
  return min + (rand() % (max - min + 1));
}
 
 
int main(void) {
  srand(time(0));               // <--- call this once at the start of the application
 
  int value = getRand(2, 120);  // <--- the random value between 2 and 120 (inclusive)
 
  return 0;
}

Open in new window

Avatar of Chrysaor
Chrysaor

ASKER

To Infinity08: What happens for the numbers I want to exclude?

To sistemu: From what I understand, in both functions rand and exclude, if the numbers we don't want are assigned to our integers, it goes from the start and assign a new random number to our integer, and it is bit time consuming. Because what I'm trying to do has time limit, and I am going to perform these random calculations million times, and the application does get a little slow, is there any other way, faster?

Thanks a lot guys..
>> To Infinity08: What happens for the numbers I want to exclude?

What is it that you want to do exactly ? How many values do you plan to exclude and why ?
I don't know any other way of excluding numbers from a random function just replacing them with other numbers.
I can't help you without further knowledge of the circumstances.

And.. yes, my random version is a little more time consuming :D
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> the problem is that if you exclude a big range

As I said in my post :

        "If the exclude list is relatively small"

If it's not, then the inverse approach like you explained is of course valid ;)    (assuming that the list of acceptable values is not too big)


It all depends on what Chrysaor intends to use this for - still waiting for his feedback ;)
>> If it's not, then the inverse approach like you explained is of course valid ;)    (assuming that the list of acceptable values is not too big)

Why not too big?

I tested with over 10,000 iterations, with a range going from 0 to 9, excluding 1 to 8. The results were either 0 or 9, and over those 10,000 iterations the odds for a 0 or 9 were near 50% (equivalent to flipping a coin). So the algorithm is poven to be mathematically and statistically precise independantly of the size of exclusions and/or inclusions.
>> with a range going from 0 to 9, excluding 1 to 8.

That's only a list of 2 acceptable values (0 and 9). That's not a big list ;)

It's just a matter of performance and memory footprint.


Again - all depends on Chrysaor's specific requirements, so I'll wait for those :)