Choosing randomly from array

I have the following numbers in an array, {0, 1, 1, 1, 2, 3} and need to randomly choose from this array until the sum of the chosen numbers equals at least 8 for the first time. For example, {2, 1, 3, 2} is acceptable, {3, 3, 3} is acceptable but something like {3, 3, 2, 1} is not acceptable because the sum equaled 8 before the '1' was chosen. This is as far as I have gotten.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
const char arrayNum[6] = { '0', '1', '1', '1', '2', '3' };

system("pause");
return 0;
}
In other words, you need to select random elements (with replacement) until the total value of elements you draw is greater-than or equal to 8?

Can you pick out a random element from the array? Since valid indices of your array are integers in [0, 5], let's see you try to generate a random number in that interval.
I have the following numbers in an array, {0, 1, 1, 1, 2, 3} and need to randomly choose from this array until the sum of the chosen numbers equals at least 8 for the first time. For example, {2, 1, 3, 2} is acceptable, {3, 3, 3} is acceptable but something like {3, 3, 2, 1} is not acceptable because the sum equaled 8 before the '1' was chosen. This is as far as I have gotten.

I don't quite understand your assignment. Can you give us more examples?
A do while -loop fits this job quite well.
The standard library has a discrete sampling distribution.
http://en.cppreference.com/w/cpp/numeric/random/discrete_distribution

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <random>

int main()
{
    std::mt19937 rng( std::random_device{}() ) ; // random number engine
    std::discrete_distribution<int> distrib( { 1, 3, 1, 1 } ) ; // sample from [ 0, 1, 1, 1, 2, 3 ]

    int sum = 0 ;
    for( int rand ; sum < 8 ; sum += rand ) std::cout << ( rand = distrib(rng) ) << ' ' ;
    std::cout << "  (sum:" << sum << ")\n" ;
}

http://coliru.stacked-crooked.com/a/4e77c76950cd3193
JLBorges, that is exactly what I am looking for, thank you so much!
Topic archived. No new replies allowed.