Array with no duplicate

Hello, I'm having some troubles with the following code. I'd like to generate an array filled with 50 random values, but something's gone wrong:

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

#include <iostream>

int
main()
{
 const int N = 50;

 int v[N];

 int i;
 for(i = 0; i < N; ++i)
 {
  int tmp_number = rand() % 50;
  std::cout << tmp_number << std::endl;

  bool found = false;
  int k;
  for(k = 0; k < i; ++k)
  {
   if(v[k] == tmp_number)
    {
     found = true;
     break;
    }
  }
 
  if(! found)
   v[i] = tmp_number;
  else
   continue;  
 }

return 0;

}


Any hint? :)

Thank you.
Last edited on
no includes
..sure. But that was part of the code. Sorry for being unclear. Now I'll modify the message, yet the problem remains.
whats wrong with it? i got 50 random nums.
1
2
3
4
5
for(i = 0; i < N; ++i)
 {
 v[i]= rand() % 50;
  std::cout << v[i] << std::endl;


i hope this will work for u......
they will be the same each time cos you need to seed random with time like so... srand(time(0))


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
 #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>

int getrand ();

int main()
{
    srand(time(0));

 const int N = 50;

 int v[N];

 int i;
 for(i = 0; i < N; ++i)
 {
  int tmp_number = getrand()%50;
  std::cout << tmp_number << std::endl;

  bool found = false;
  int k;
  for(k = 0; k < i; ++k)
  {
   if(v[k] == tmp_number)
    {
     found = true;
     break;
    }
  }

  if(! found)
   v[i] = tmp_number;
  else
   continue;
 }

return 0;}

int getrand ()
{
    rand();
}



ignore the extra includes couldnt remember <ctime>

EDIT:well you wanted nice tarty display like aqeel made you or genuine reliable different random numbers each time :(
Last edited on
It was more an "academic" exercise than a real useful one. What I needed was to generate a random number -no larger than 50- checking if the number was already stored in the array and if it was not adding it, else computing another random number and so on.

I've solved using while instead than for. It sounds as if I can't use the instruction continue with for, or at least it does not do what I was meant to make it does (i.e. aborting that step and continuing to cycle).

Here's how I've solved that:

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
int
main()
{
 const int N = 50;

 int v[N];

 int i = 0;
 while(i < N)
 {
  int tmp_number = rand() % 50;

  bool found = false;
  int k;
  for(k = 0; k < i; ++k)
  {
   if(v[k] == tmp_number)
    {
     found = true;
     break;
    }
  }
 
  if(found)
   continue;
  else
   {
    v[i] = tmp_number;
    ++i;
   }
 }

#ifdef DEBUG
 for(i = 0; i < N; ++i)
  std::cout << v[i] << " ";
 std::cout << std::endl;
#endif

 return 0;
}


Is it that a good way to deal with that?

Thank you in advance.
yeah is good...not random each time though :P
Last edited on
@giuscri

Here is a way to place a random number into each array element, without having any repeats.

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
// 50 Random Numbers.cpp : main project file.

#include <iostream>
#include <windows.h>

int main()
{
	CONST int NUM = 50; // Change 50 to how many different numbers are needed 
	int v[NUM]={0},rnd;
	srand(GetTickCount());
   for (int i=1;i<=NUM;i++)
   {
	   do
	   {
		   rnd = rand()%NUM;
	   } while(v[rnd]>0);
	   v[rnd]=i;
   }

   for(int i=1;i<=NUM;i++)
   {
	   std::cout << v[i-1] << " ";
	   if(i%10 == 0)
		    std::cout << std::endl;
   }

return 0;
}
Topic archived. No new replies allowed.