3

I have a list of integers intList = {1, 3. 5. 2} (Its just an example integer and size both are unknown). I have to chose a random number from that list.

RandomInt = rand() % intList.size() 

will work in a similar way as RandomInt = rand() % 4

and generate a randon number between 1 to 4. while intList is different.

If I am using RandomInt = std::random_shuffle = (intList, intList.size()) still getting error. I do not know how chose a random number from a list.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
noman
  • 65
  • 1
  • 1
  • 6
  • RandomInt = std::random_shuffle(intList, intList.size()); – Shankar Shastri Sep 29 '16 at 06:06
  • 2
    `std::random_shuffle` takes a range from [first, last) with the optional additional argument of a functor. It also returns `void`. A proper call looks like `std::random_shuffle(intList.begin(), intList.end())` – The Eyesight Dim Sep 29 '16 at 06:22

3 Answers3

10

Since, as a substep, you need to generate random numbers, you might as well do it the C++11 way (instead of using modulo, which incidentally, is known to have a slight bias toward low numbers):

Say you start with

#include <iostream>
#include <random>
#include <vector>

int main()
{
    const std::vector<int> intList{1, 3, 5, 2};

Now you define the random generators:

    std::random_device rd; 
    std::mt19937 eng(rd());
    std::uniform_int_distribution<> distr(0, intList.size() - 1);

When you need to generate a random element, you can do this:

    intList[distr(eng)];
}
Community
  • 1
  • 1
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • 2
    You might want to mention that this will have vastly superior statistical properties to the rand and mod way. – Bathsheba Sep 29 '16 at 06:24
  • @Bathsheba Thanks! Just added a link about the known bias. Was that what you meant? – Ami Tavory Sep 29 '16 at 06:24
  • I don't think the random number generators are a good choice for this answer. They're a pathetically and pointlessy hard-to-use addition to C++ and one should use them only if forced. Not as pathetic and pointlessy hard to use as the time cra^H^H^Hstuff, but not far from that. – 6502 Sep 29 '16 at 06:24
  • @6502 I think that they're good practice - the % is known to have a bias (albeit a slight one). UVd your answer too, though, because it's fine as a "quick and dirty" way (no offense meant). – Ami Tavory Sep 29 '16 at 06:26
  • @6502 What's hard to use about it? After the initial setup (3 lines of code), It's not like we have to see or know about the implementation, so it's really as easy to use as `int i = giveMeARandom(eng)`. It's even easier if one wraps it in some function. – The Eyesight Dim Sep 29 '16 at 06:27
  • @TheEyesightDim: then give me a `giveMeARandom` call instead of (in addition to) this crap. **Forcing** me to write (and read and memorize) about these often-very-pointless details is good for nothing except for the ego of the authors of this part. – 6502 Sep 29 '16 at 06:30
  • @6502 Those details are only pointless if you don't care about them in your particular problem. Having a non-deterministic generator, a high quality built in algorithm, and a way of changing the distribution type and range are all useful to quite a few people. – The Eyesight Dim Sep 29 '16 at 06:37
  • @6502: Have a look at Java's date and calendar libraries if you want to see true abysmality ;-) Personally though I think the new random number generator library is extremely well-designed. – Bathsheba Sep 29 '16 at 06:51
  • @TheEyesightDim: for reasons that are beyond my understanding a few people get obsessed with the pseudo-random problem like if it was the most important problem for computer science. I think it's ok that they go down that path and I've no problem with this. The problem is when this mania gets to the standard where indeed for most uses just having a single "rand()" returning a number uniformly between 0 and 1 would have been sufficient. I'm not against having a gajillion of distributions and paranoias to chose from... just give me "#include " and "std::rand()" if I don't care. – 6502 Sep 29 '16 at 13:06
7

You just need to use "indirection":

std::vector<int> list{6, 5, 0, 2};
int index = rand() % list.size(); // pick a random index
int value = list[index]; // a random value taken from that list
6502
  • 112,025
  • 15
  • 165
  • 265
0
  int arrayNum[4] = {1, 3, 5, 2};
  int RandIndex = rand() % 5; // random between zero and four
  cout << arrayNum[RandIndex];
rocambille
  • 15,398
  • 12
  • 50
  • 68
Wesam
  • 932
  • 3
  • 15
  • 27