6

How would you make a function that generates a random number from 1 to 25 million?

I've thought about using rand() but am I right in thinking that the maximum number, RAND_MAX is = 32000 (there about)?

Is there a way around this, a way that doesn't reduce the probability of picking very low numbers and doesn't increase the probability of picking high / medium numbers?

Edit: @Jamey D 's method worked perfectly independent of Qt.

Alarming
  • 137
  • 1
  • 3
  • 14
  • 4
    You can use the utilities in [``](http://en.cppreference.com/w/cpp/header/random) to do this. – NathanOliver Dec 10 '15 at 19:04
  • [```RAND_MAX```](http://en.cppreference.com/w/cpp/numeric/random/RAND_MAX) is implementation dependent. It's guaranteed that this value is at least ```32767```. – mepcotterell Dec 10 '15 at 19:04
  • @Nathan that should be an answer – Russell Greene Dec 10 '15 at 19:04
  • @RussellGreene That would be a poor answer in my opinion. As is this question is too broad – NathanOliver Dec 10 '15 at 19:05
  • @Nathan I meant that but with an example. That is the "standard" way to do it. – Russell Greene Dec 10 '15 at 19:06
  • What's the purpose of the `qt` tag? – skypjack Dec 10 '15 at 19:08
  • @skypjack OP seems to not know how to do it in QT. Fair question - *a priori* the standard solution might be in QT and other QT developers may need to know how to do this. – djechlin Dec 10 '15 at 19:11
  • @djechlin he is using `rand` and not `qrand`. He says that he uses `QtCreator`, but it doesn't allow him to use the `qt` tag for such a question... I use the same ide to develop in almost each language at works!! My two cents: not about `Qt` at all. – skypjack Dec 10 '15 at 19:15
  • @skypjack he says he's not sure if he should be using rand() but is not sure since it caps out ~32,000. Does qrand cap out around 32,000? If not then that would be an excellent answer to this question. OP probably didn't even know about qrand. – djechlin Dec 10 '15 at 19:16
  • @djechlin `qrand` is all about defining a thread safe version of `rand`, unfortunately it suffers of the same limitation (that is `RAND_MAX`). – skypjack Dec 10 '15 at 19:19
  • I included Qt as a tag because I had initially opened this question up to get c++ but also qt-unqiue methods of doing this, yes `qrand()` existed but I found out that it did have the same limitation as `rand()` which was also pointed out by @skypjack. I guess there is no other Qt-specific method of doing so and that was what I wanted to find out too. – Alarming Dec 10 '15 at 19:40
  • If you had away to get three digit random numbers but needed a six-digit random, you'd just combine two of them. So something like: `((rand() * RAND_MAX) + rand())` – David Schwartz Dec 10 '15 at 19:46

2 Answers2

12

You could (should) use the new C++11 std::uniform_real_distribution

#include <random>

std::random_device rd;
std::mt19937 gen(rd());

std::uniform_real_distribution<> distribution(1, 25000000);

//generating a random integer:
double random = distribution(gen);
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

Have a look at ran3

http://www.codeforge.com/read/33054/ran3.cpp__html

You should be able to get what you want from it.

Ran3 is (atleast when I was still doing computational modelling) faster than rand() with a more uniform distribution, though that was several years ago. It returns a random integer value.

For example, getting the source code from the link above:

int main() {
   srand(time(null));

   int randomNumber = ran3(rand()) % 25000000;
   int nextRandomNumber = ran3(randomNumber);
}
Ajwhiteway
  • 986
  • 1
  • 9
  • 23
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – NathanOliver Dec 10 '15 at 19:09
  • 1
    Yeah, I was picking away at the edit while waiting on compiling code. Its up now. Likely would recommend the C++11 answer over the one I gave unless you need really good random numbers though. – Ajwhiteway Dec 10 '15 at 19:34
  • 1
    Commonly, you cant use modulo operation to scale interval of random number generation. Resulting distribution becomes nonuniform. – Semyon Burov Aug 30 '16 at 14:40
  • http://stackoverflow.com/a/10984975/1628385 is what Semyon is referring to. There is an answer there on how to avoid Modulo Bias. – Ajwhiteway Aug 30 '16 at 15:05