I want to generate 10 random numbers. but all numbers will be different. so I tried to
use bool first make all false. and each generate, assign that argument with bool
but someting is wrong. because outcome is not correct.

``````#include <cstdlib>
#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;

void sifirla(bool sayi[]); // making zero
int sayisec(bool sayi[]); // choosing number from random seed
int xx[11]; // random numbers will be assigned to xx array.

int main(int argc, char *argv[])
{
bool sayi[10];
time_t qTime;
time(&qTime);
srand(qTime);

sifirla(sayi);
for (int pp=1;pp<11;pp++)
{
xx[pp]=sayisec(sayi);
cout << pp<<".) "<<xx[pp]<<"\n";
}
system("PAUSE");
return EXIT_SUCCESS;
}

void sifirla(bool sayi[])       // all values will be false....
{
for (int x=0;x<11;x++)
{
sayi[x]=false;
}
}
int sayisec (bool sayi[])     // choose random number between 1-10
{
bool sayii = true;
int secilen_sayi = -5;
do {
secilen_sayi=(rand() %10)+1;
if (!sayi[secilen_sayi])
{
sayii=false;
}
}while (sayii);
return secilen_sayi;
}``````

Have you had a look at this reference? It describes how to use rand and srand well.

I notice on line 9 of your program you have your array declared with 11 …

Note: `bool sayi[10] = {false};` will initialize the whole array to false. So there is no need for sifirla function. Its hard for me to understand your code.

I suggest you do something like the following :

``````int randRange(int min, int max){
return rand() % …``````

## All 5 Replies

Have you had a look at this reference? It describes how to use rand and srand well.

I notice on line 9 of your program you have your array declared with 11 values. Why is this? I thought you wanted only 10?

From your description it seems as though you want to generate 10 random numbers between 1 and 10. So couldn't you just generate each number and use a couple loops and an if statement or two to check if the number has already appeared in the array? You could have the loop keep trying to generate numbers until all of them are unique.

Note: `bool sayi[10] = {false};` will initialize the whole array to false. So there is no need for sifirla function. Its hard for me to understand your code.

I suggest you do something like the following :

``````int randRange(int min, int max){
return rand() % (max-min) + min;
}

//returns a vector with unique random number int range [minValue,maxValue)
//the size parameter declares the size of the vector being returned
std::vector<int> generateUniqueRandom(int minValue, int maxValue, int size){
assert(minValue >= 0 && maxValue >= 0);
assert(minValue < maxValue);
assert(size <= maxValue - minValue );
assert(size >= 0);

std::vector<int> randomValueList;

//populate vector with possible random values
for(int i = minValue; i != maxValue; ++i)
randomValueList.push_back(minValue);

//now distort their position randomly
std::random_shuffle(randomValueList.begin(), randomValueList.end());

//now make randomValueList the appropriate size
int cuttofRange = randomValueList.size() - size;

randomValueList.erase( randomValueList.begin() + cuttofRange, randomValueList.end() );

return randomValueList;
}``````

The code above is not compiled, but it gives you a way to approach it. It might need some tweaking though.

Note: `bool sayi[10] = {false};` will initialize the whole array to false. So there is no need for sifirla function. Its hard for me to understand your code.

I suggest you do something like the following :

``````int randRange(int min, int max){
return rand() % (max-min) + min;
}

//returns a vector with unique random number int range [minValue,maxValue)
//the size parameter declares the size of the vector being returned
std::vector<int> generateUniqueRandom(int minValue, int maxValue, int size){
assert(minValue >= 0 && maxValue >= 0);
assert(minValue < maxValue);
assert(size <= maxValue - minValue );
assert(size >= 0);

std::vector<int> randomValueList;

//populate vector with possible random values
for(int i = minValue; i != maxValue; ++i)
randomValueList.push_back(minValue);

//now distort their position randomly
std::random_shuffle(randomValueList.begin(), randomValueList.end());

//now make randomValueList the appropriate size
int cuttofRange = randomValueList.size() - size;

randomValueList.erase( randomValueList.begin() + cuttofRange, randomValueList.end() );

return randomValueList;
}``````

The code above is not compiled, but it gives you a way to approach it. It might need some tweaking though.

unfortunately I dont understand your code. it seems high level. I dont know assert function and also some function is so unfamiliar for me.

what I wanted to do in my program , first I created a array of 10 items. I assigned false to each item. and later product random and changed that item to "bool true" thus trying to create unique random 10 numbers. but could not achieve it.

I think you're making the problem too complicated for yourself.

Try something like this.

``````int random[10];

for (int i = 0; i < maxValue; i++)
{
//assing random value to random[x];
for (int x = 0; x < maxValue; x++)
{
if (random[i] == random[x])
{
//try to assign another random value
// make x = 0 again so it will check the new number with the whole array again
}
}
}``````

I think you're making the problem too complicated for yourself.

Try something like this.

``````int random[10];

for (int i = 0; i < maxValue; i++)
{
//assing random value to random[x];
for (int x = 0; x < maxValue; x++)
{
if (random[i] == random[x])
{
//try to assign another random value
// make x = 0 again so it will check the new number with the whole array again
}
}
}``````

you are so genius. I could not even think of it. I did not compile and modify your program but the concept of the program is right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.