Hi ladies and gentlemen,

Gotta question,

I have a program in wich I make a small array of 100 random numbers wich are smaller then 1000!

Like this:

# define MAX 100

void srand(int tabel[MAX]);

void srand(int tabel[MAX])
    {
        for (int i=0;i<MAX;i++)
            do
            tabel[i]=rand();
            while (tabel[i]>1000);  
    }

This of course is only one part of the program and as you can see this array is made with a function! Now, this way I get random numbers but there are several numbers in that array wich are shown twice or even more times, how can I change this program so it doesn't use the same number twice?

Can anyone please help me out?

Thanks in advance!

One solution can be, before inserting the new no. in the array, scan the array (iterate through it), to see whether the no. is already in the array or not, if not, insert it and yes, loop again.

You can either iterate by simple loop or if you manage to create the array in sorted order, then you can use binary search (which is much faster) to serve the purpose.

If you can use STL, then you can use vector and find algorithm, which can make your life much easy.

Hi Ejaz,

Thanks for the quick reply, but can you give me an example how I should write it because that's the problem, I understand that I have to use an iteration to control every single N., but how do I write this combination of iteration and selection :?:

Thanks

The simple and not so great way of iterating through the array to see if it is a duplicate and discarding it may go something like this.

#include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
 
 #define SIZE 100
 #define MAX  1000
 
 int main(void)
 {
    int array[SIZE];
    size_t i,j;
    srand(time(NULL));
    for ( i = 0; i < sizeof array / sizeof *array; ++i )
    {
 REDO: array[i] = rand() % MAX;
 	  for ( j = 0; j < i; ++j )
 	  {
 		 if ( array[i] == array[j] )
 		 {
 			goto REDO;
 		 }
 	  }
 	  printf("%3d%c", array[i], i % 10 == 9 ? '\n' : ' ');
    }
    return 0;
 }
 
 /* my output
 138 327 659 825 356 700 248 195 615 253
 318 123 215 333 396 813 196 230 376 725
 139 446 985 816 383 427 557 411 523 277
 270 943 869 200 690 154 345 912 239  94
 937 374 882 665 762 938 681 956 961 673
 838 119 526 234 549 932 304 579 617 755
 578 409 543 766 727 571 471  48 574 394
 614 973 421 528 142 804 115 530 338 788
 691 688 883 878 887 350 661  64 998 749
 572 216 513  89 441 243 952 209 468 959
 */

Thanks very much Dave :p

This way I can see how you did it, and find a way to do it in my programm :)

Got a few questions tough, I don't want to just copy your solution without knowing why you did certain things and what the meaning of some of them is :!:

1) srand(time(NULL)); Ive read in a tutorial on this forum about it, does this make certain that the random numbers are allways different? Why are there two )'s at the end, think this was not intentional right :?:

2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these :?:

3) REDO, I understand the English words and can see what it does, but can you tell me what it is :?: Meaning, could I use another word to get my variable into the iteration again :?: IT's probably and most certainly the goto that does it right, but just want to be certain :)

4) When you use array[ j ], this is just the next number in the array wich comes after [ i ] correct and could you use something like this:

if ( array == array -1) because this way you are referring to the previous number in the array aswell correct :?:

Anyway, thanks again for the example, it's going to help alot, if you don't have time to explain these questions, I understand :!:

I don't want to just copy your solution without knowing why you did certain things and what the meaning of some of them is :!:

Bless you.

1) srand(time(NULL)); Ive read in a tutorial on this forum about it, does this make certain that the random numbers are allways different? Why are there two )'s at the end, think this was not intentional right :?:

srand[b]([/b]time[b]([/b]NULL[b]))[/b];

2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these :?:

To get the number of elements. Total array size in bytes divided by the individual element size in bytes yields the number of elements.


3) REDO, I understand the English words and can see what it does, but can you tell me what it is :?: Meaning, could I use another word to get my variable into the iteration again :?: IT's probably and most certainly the goto that does it right, but just want to be certain :)

It is the label that the goto goes to.

REDO: array[ i ] =rand() % MAX;
      	  for ( j = 0; j < i; ++j )
      	  {
      		 if ( array[ i ] == array[ j ] )
       		 {
       			goto REDO;
       		 }

4) When you use array[ j ], this is just the next number in the array wich comes after [ i ] correct and could you use something like this:

if ( array[ i ] == array[ i ] -1) because this way you are referring to the previous number in the array aswell correct :?:

The counter j goes from zero to the current value of i.

for ( i = 0; i < sizeof array / sizeof *array; ++i )
           {
        REDO: array[ i ] = rand() % MAX;
        	  for ( j = 0; j < i; ++j )
 	 {

Just think of and example case when you're partway through. Let's say i is 40: you've already filled up array[0] to array[39] and just obtained a new candidate for array[40]. You'll want to compare array[0], array[1], array[2], ... to make sure that the current pick for array[40] isn't the same as any of the previous ones. If array[40] is the same as array[22], you'll want to redo the random value.

I really had trouble in finding the solution, Ive tried to find a solution on my own, but somehow I just couldn't see how I had to do it :-|

The srand ( , I saw this a bit to late :lol:

I don't only know how to solve the problem now, but also understand why you did it, thanks again :!:

Be a part of the DaniWeb community

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