number generator questions



#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;

void gen(int num[]) {
for (int x=0; x<10;x=x+1) {
num[x] = x+1;
}
random_shuffle(&num[0], &num[10]);
int x;
for (x = 0; x<6; x++) { // selection of six numbers
cout <<num[x] << ” “;
}
}

int main()
{

const int rows = 2;
const int cols = 6;
int excempt[rows][cols] = {{2,4,6,5,1,3}, // how to avoid this set of numbers
{1,3,5,7,2,4}}; // when generating new sets ?
int numbers[10];
srand( time(NULL));
gen(numbers);
cout << endl;

return 0;
}

// hi to all.. i have something to ask ,,
// how to avoid/ excempt arrays of data in number generations on c++? (as i have stated on my codes on line 24 and 25.)
// thanks for helping ,, , sorry for my english.
I don't know of any way to tell the random number generator to not return certain numbers.
If there is a way, I'd love to hear of it.

After you've filled your array, go through and find the numbers you don't want. Generate a new random number and replace it, and keep going until the undesired numbers aren't present anymore. Though, you could probably write a wrapper for a random number generator that would do this for you without too much difficulty.
It does look like that you do want to get 6 unique random values that are picked from a set of unique values and you have a list of values that are not allowed to be in the set. (I.e. will never be picked.)

Is that the problem?

When creating the set, do not add values that you don't want. Your "set generation" is apparently:
1
2
3
for (int x=0; x<10;x=x+1) {
  num[x] = x+1;
}

Which adds values [1..10] to the set. If you do excempt either row of your array from them, then you will have only 4 values, but you do pick 6 ...

PS. srand() and std::random_shuffle() are deprecated functions that can/will vanish from C++ in the future. Start using std::shuffle() instead.
Keskiverto thanks for the reply... ive made a loop for for this number generator... and i dont want to appear the sets of six numbers that i have added in excempt array... but i dont have any idea on how to avoid that sets from appearing in the loop
Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<int> excempt = ...
std::sort( excempt.begin(), excempt.end() );

size_t wanted = 12;
std::vector<int> set;
set.reserve( wanted );

int number = 7;
while ( set.size() < wanted ) {
  if ( ! std::binary_search( excempt.begin(), excempt.end(), number ) ) {
    set.push_back( number );
  }
  ++number;
}
// Set has 12 values from 7, 8, 9, ..  (except the excempted)
// now shuffle and ... 


@JayhawkZombie: the principle in OP is similar to lottery or card game:
* First you take the numbered balls or deck of cards (which can be ordered)
* Then you throw away some
* Shuffle the remaining balls/deck
* Draw first N balls/cards. They are random due to shuffle
Last edited on
Keskiverto thanks to your given code,,, i appreciate it much... im just a newbie in c++ ,,, again thank you
Oh, just noticed (and edited away) logical error from the sample.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 void gen(int num[]) {
    for (int x=0; x<10;x=x+1)  {
     num[x] = x+1;
    }
   random_shuffle(&num[0], &num[10]);
   int x;
   for (x = 0; x<6; x++)  {  // selection of six numbers

}
}


void gendisp(int num[]) {
    for (int x=0; x<10;x=x+1)  {
     num[x] = x+1;
    }
   random_shuffle(&num[0], &num[10]);
   int x;
   for (x = 0; x<6; x++)  {
cout <<num[x] << " ";
}
}



int main()
{

    const int rows = 2;
    const int cols = 6;
    int excempt[rows][cols] = {{2,4,6,5,1,3}, // how to avoid this set of numbers
                               {1,3,5,7,2,4}};

   int numbers[10];
   for(int i = 0; i < 10; ++i) {
    cout << endl;
    gen(numbers);
   if(excempt == gen(numbers))
   return 0;
   else
    gendisp(numbers);
}
return 0;
}

// error has occured...
// invalid operands of types ‘int [2][6]’ and ‘void’ to binary ‘operator==’ 


@Keskiverto... Sir, can you take a look at my code... ive got an invalid operands at line 38 when running this code.
i hope somebody will help me for this... im just a beginner in c++..
Topic archived. No new replies allowed.