0

I am writing function where I need to find the random number between 1 - 10. One of the easiest way is to use random() libc call. I am going to use this function a lot. But I don't know how efficient it will be. If any one has idea about efficiency of random() that will be a help ?

Also I notice that random() give the same pattern in 2 runs.

int main()
{
   for(int i=0;i<10;i++)
   {
    cout << random() % 10 << endl;     
   }
}

Output 1st time :- 3 6 7 5 3 5 6 2 9 1

Second time also I got same output.

Then how come it's random ?

eswaat
  • 733
  • 1
  • 13
  • 31
  • Use a profiler if you're worried about performace. If you run the randomizer with the same seed, it will always give the same output. – Almo Jan 31 '15 at 18:53
  • Actually I used Linux perf but there it's is not showing anything (like where it's consuming time). It's just showing how much CPU cycle it consume and it was around 1.73% of total CPU. – eswaat Jan 31 '15 at 18:57
  • 1
    "efficiency" as in "performance" or "efficiency" as in "how good are the random numbers"? The latter is clearly a case of "you need to use srand()" [or similar] to start the random number sequence at a different point. "Good" random numbers is not trivial, so you may need further work if you need truly good random numbers [e.g. games where predictability becomes an issue, or using the numbers for scientific purposes where the distribution is important] – Mats Petersson Jan 31 '15 at 19:01
  • Any reason for negative marking. – eswaat Jan 31 '15 at 19:18

4 Answers4

6

Others have explained why it's the same sequence every time, but this is how you generate a random number with C++:

#include <random>

int main() {
    std::random_device rd{}; //(hopefully) truly random device
    std::mt19937 engine{rd()}; //seed a pseudo rng with random_device
    std::uniform_int_distribution<int> d(1,10); //1 to 10, inclusive
    int RandNum = d(engine); //generate
    return 0;
}

http://en.cppreference.com/w/cpp/numeric/random

Quentin
  • 62,093
  • 7
  • 131
  • 191
Blob
  • 561
  • 1
  • 6
  • 19
2

The actual execution time depends on your platform of course, but it is pretty much straight forward, couple multiplication and divisions or shifts:

What common algorithms are used for C's rand()?

I don't think you should be worried. If you need a lot of random numbers, then another random source probably would be a better choice for you.

If you are looking for tweaks, how about splitting the result from rand() into individual digits to get several results per call.

Community
  • 1
  • 1
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
  • The idea of splitting the result from `rand()` into individual digits (say, blocks of 4 bits to get random numbers from 1-10, say, by ignoring 0000, 1111, 1110, 1101, 1100 and 1011) is an interesting one but it would be important to [test the quality of the random numbers](http://en.wikipedia.org/wiki/TestU01) generated this way because the lower bits in most pseudo-random numbers are typically less random than the higher-valued bits. – Simon Jan 31 '15 at 20:25
0

This way is very simple and effective, you only need to set the seed:

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

using namespace std;

int main(){
    srand(time(NULL));
    for(int i=0;i<10;i++)
        cout << rand() % 10 << endl;     
}
Mayke Ferreira
  • 174
  • 2
  • 9
0

To fix the problem of getting same pattern in 2 runs just add the function randomize()

Arun A S
  • 6,421
  • 4
  • 29
  • 43