### Help with a little function

Hi all!
i'm new on this forum and quiete new to programming xD
i've create this piece of program:

for ( int i = 0 ; i < 4 ; i++ ) {
hold = rand()%9 + 1;
for ( int j = 0 ; j < i ; j++ ) {
if ( hold = comb[j] )
hold = rand()%9 + 1;

}
comb[i] = hold;
}

in which an array with 4 elements is filled with random number (1,2,3,4,5,6,7,8 or 9). Actually there's a problem: i need them all different.

But that don't works =( could you help me?
thx a lot!!

ps: sorry for mistakes in writing xD
Last edited on
I don't really understand your question;
but if the problem is that u are not getting random numbers,that's because you need to use srand(), before using rand().

do this before rand();
srand(static_cast<unsigned int>(time(0))); // this line provides a seed for the function rand(), otherwise rand will always give out the same numbers.

You are using the assignment operator instead of the comparision operator in the statement

if ( hold = comb[j] )
hold = rand()%9 + 1;

}

Change it to

if ( hold == comb[j] )
hold = rand()%9 + 1;

}
arrgg!!! that was so easy!! thx very much vlad!
However take into account that your code in any case is invalid.
Let assume that

hold != comb[0];

but

hold == comb[1];

So you are changing hold. Now

hold != comb[1];

but in can become equal to comb[0] !:)
Last edited on
again, that's right...i'll think on it...
truely, thx =)

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include #include /* srand, rand */ #include bool isFind(const std::vector& data, int hold) { for ( int i = 0; i < data.size(); ++i) { if ( hold == data[i] ) { return true; } } return false; } int main() { int hold; std::vector data; srand (time(NULL)); while( !(data.size() == 4) ) { hold = rand()%9 + 1; if ( !isFind(data, hold) ) data.push_back(hold); } for (int i = 0; i < data.size(); ++i) { std::cout << data[i] << " "; } std::cout << std::endl; std::cin.get(); return 0; }``````
no, wait vlad: i've avoided this possibility with the second counter!

the second "for" create a loop in which hold is compared to every antecedent elements. only after the whole comparison vett[i] is filled with hold...

am i still wrong?
I think you are wrong.
Let assume that comb[0] == 1 and comb[1] == 2. You are trying to assigne comb[2]; For example you have got hold = 2. Then

comb[0] != hold

All is o'k.

But comb[1] == hold

So you are getting new hold. For example the new hold == 1. So you are assigning comb[2] = 1 that is now comb[2] == comb[0]
@CroCo

 ``1234`` ``````#include #include #include /* srand, rand */ #include ``````

There's also a pseudo-random engine called mt19937, which you can instantiate objects of.

 ``123456789`` ``````#include #include // std::mt19937 #include int main() { std::mt19937 en(std::time(nullptr)); std::cout << en(); }``````

I suggest using this version of random engine because I think std::rand() is global. I'm afraid this'll spread the random number out over multiple classes or threads that need random numbers, which may affect randomness.
@Bourgond Aries,
Yes, you are right about the header files. Thanks for random engine.
Topic archived. No new replies allowed.