3

I want to be able to generate random numbers from a specific array that I will place. For example: I want to generate a random number from the array {2,6,4,8,5}. its just that there is no pattern in the array that I want to generate.

I was only able to search how to generate a random number from 1-100 using srand() from the video tutorial https://www.youtube.com/watch?v=P7kCXepUbZ0&list=PL9156F5253BE624A5&index=16 but I don't know how to specify the array that it will search from..

btw, my code is similar to this..

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, char*argv[])
{
    srand(time(0)); 

    int i =rand()%100+1;
    cout << i << endl; 
    return 0;
}
Funky
  • 41
  • 4
  • 1
    Off topic because this is inefficient as heck for this case, but very useful in others. [std::random_shuffle](http://en.cppreference.com/w/cpp/algorithm/random_shuffle) the array ([see here for how](http://stackoverflow.com/questions/14720134/is-it-possible-to-random-shuffle-an-array-of-int-elements)) an then take `array[0]`. This is a great way to have a pool of non repeating random numbers. – user4581301 Aug 26 '16 at 08:53

3 Answers3

5

Here is a modern C++ way to do it:

#include <array>
#include <random>
#include <iostream>

auto main() -> int
{
    std::array<int, 10> random_numbers = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };

    std::random_device random_device;
    std::mt19937 engine(random_device());
    std::uniform_int_distribution<int> distribution(0, random_numbers.size() - 1);

    const auto random_number = random_numbers[distribution(engine)];
}

You can read more about the C++ random functionalities from the standard library here: http://www.cplusplus.com/reference/random/

CppChris
  • 1,226
  • 9
  • 14
  • Hi! can I ask what the mt19937 engine(random_device()); for? and also the std::uniform_int_distribution distribution(0,random_numbers.size() - 1); thank you!! – Funky Aug 26 '16 at 08:38
  • 1
    @Funky it's explained in the excellent video linked to in another answer: http://stackoverflow.com/a/39161875/2920343 – CompuChip Aug 26 '16 at 08:39
  • 1
    Right way to do this, but watch out for random_device if using stock mingw. It always returns the same number. Not all that random a device. – user4581301 Aug 26 '16 at 08:39
  • 1
    These classes are also all described in the [ documentation](http://www.cplusplus.com/reference/random/). – Michael Aug 26 '16 at 08:40
  • @Funky: the engine generates uniformly distributed numbers and you specify the range of this distribution to be from 0 as the index of the first element in the array to array.size() - 1 as the last element in the array. Don't forget "-1" or you might access your array out-of-range. – CppChris Aug 26 '16 at 08:41
  • @user4581301 didn't know this, thanks for the hint! What is the right way to generate random numbers in C++11 with mingw? – CppChris Aug 26 '16 at 08:42
  • @ChrisG Thank you!! I was wondering if I can implement the 2 sec rule before it change like the one describe in http://stackoverflow.com/questions/39158289/generate-a-random-number-every-2-seconds-c-srand – Funky Aug 26 '16 at 08:44
  • @Funky I am not sure if I understand correctly, but if you want to generate a random number different to the previous one on every run/loop/call, you could just remember the last picked random number, compare the current picked random number with it and if it is the same, pick randomly a new index from a range without the index of the previous random number (for example, this new index would be added to the previous and "wrap around") – CppChris Aug 26 '16 at 08:56
  • @Funky EDITED just look at user4581301 's comment to your question, random_shuffle might be the way to go, I did not know you needed them non-repeating – CppChris Aug 26 '16 at 08:58
  • 1
    @ChrisG I don't have a good answer for that, unfortunately. I've been throwing a millisecond count from chrono's high resolution clock in. – user4581301 Aug 26 '16 at 09:29
0

generate random index for this arrays:

before you make a random value, let's init 'the system':

srand((unsigned int)time(0)); // somewhere in the beginning of main, for example

then you somewhere initialize you array, let's say like this:

std::vector<int> array;
fillOutArray(array);

you got something like in you first message: {10, 5, 3, 6}

now you want to get a random value from this array (among these 10, 5, 3 or 6):

auto index = rand() % (array.size());
auto yourValue = array[index];

that's just it.

fgrdn
  • 91
  • 1
  • 5
  • Hi! sorry I didn't quite get it?? the number that I want to generate are actually those 5. but where will I place them in the code that you've provided?? Sorry I'm new to this – Funky Aug 26 '16 at 08:30
  • @Funky edit my message a little. probably now it's more clear – fgrdn Aug 26 '16 at 08:56
0

Using modulus to change your output range can introduce a slight bias. See this talk. If this is a concern of yours, consider using the "random" standard library instead since you're using c++.