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;
}
1
  • 1
    Off topic because this is inefficient as heck for this case, but very useful in others. std::random_shuffle the array (see here for how) an then take array[0]. This is a great way to have a pool of non repeating random numbers. Commented Aug 26, 2016 at 8:53

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

10 Comments

Hi! can I ask what the mt19937 engine(random_device()); for? and also the std::uniform_int_distribution<int> distribution(0,random_numbers.size() - 1); thank you!!
@Funky it's explained in the excellent video linked to in another answer: stackoverflow.com/a/39161875/2920343
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.
These classes are also all described in the <random> documentation.
@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.
|
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++.

2 Comments

True, but a comment not an answer. Can be salvaged by expanding the explanation.
Couldn't comment without having 50 rep. No need to salvage though, other answers suffice.
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.

2 Comments

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 edit my message a little. probably now it's more clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.