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().

header files are:<ctime>and <cstdlib>

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 =)

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
#include <vector>
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <time.h>  


bool isFind(const std::vector<int>& 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<int> 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

I believe in C++ we use c* headers instead of *.h.

1
2
3
4
#include <vector>
#include <iostream>
#include <cstdlib>     /* srand, rand */
#include <ctime>   


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

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <random> // std::mt19937
#include <ctime>

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.