-3

Hello friends I need your help.

My program is such an array size 1000 where the numbers should be between 0-999. These numbers should be determined randomly (rand loop) and the number must not be repeated. Would be considered the main part, I have to count how many times I used rand().

My idea is that: one loop where it initializes all the 1000 numbers, and if in this loop they check whether the number appears twice, if the number appears twice is set it again until that not appear twice (maybe this is not the best way but ...)

It is my exercise (Here I need your help)-

#include <stdio.h>
#include <stdlib.h>
int main()
{
int const arr_size = 1000;
int i, j, c;
int arr[arr_size];
int loop = 0;

for(i = 0; i<arr_size; i++)
{
 arr[i] = rand() % 1000;
loop++;
if (arr[i] == arr[i - 1])
{
arr[i] = rand() % 1000;
loop++;
}
}
printf("%d\n",loop);
}

So if anyone can give me advice on how I can make it work I appreciate your help. Thanks.

  • 2
    If you want n non-repeating random numbers in the range 1...N, I would say its better to shuffle a sequence 1...N and then pick the first n numbers from the shuffled sequence. For answer on shuffling see here: http://stackoverflow.com/questions/6127503/shuffle-array-in-c – Brandin Dec 29 '13 at 17:10
  • 1
    For your idea to work, you need to check *every* value you already have. As it is, you only check the one before the current: `arr[i-1]`. (And you should not do that on the first element.) But... can you imagine what happens when you are near the end of your 1,000 element loop? – Jongware Dec 29 '13 at 17:10
  • Your idea will work but it will probably take a very long time to execute, possibly longer than the assignment has to run. You will need a method of selecting numbers from a set of not-yet-used numbers, or to shuffle the array some way. – Tim Pierce Dec 29 '13 at 17:11
  • So can anyone give me an example of the method can work faster – user3144308 Dec 29 '13 at 17:14
  • possible duplicate of [Is this C implementation of Fisher-Yates shuffle correct?](http://stackoverflow.com/questions/3343797/is-this-c-implementation-of-fisher-yates-shuffle-correct) – Hans Passant Dec 29 '13 at 17:17

2 Answers2

0

As suggested, shuffling the set will work but other indirect statistical quantities might be of interest, such as the distribution of the loop variable as a function of the array index.

This seemed interesting so I went ahead and plotted the distribution of the loop as a function of the array index, which generally increases as i increases. Indeed, as we get near the end of the array, the chance of getting a new random number that is not already in the set decreases (and hence, the value of the loop variable increases; see the code below).

Specifically, for an array size = 1000, I recorded the non-zero values generated for loop (there were around 500 duplicates) and then made a plot vs the index.

The plot looks like this:

enter image description here

The code below will produce an array with the unique random values, and then calculate the value for loop. The loop values could be stored in another array and then saved for later analysis, but I didn't include that in the code below.

Again, I'm not exactly sure this fits the application, but it does return information that would not necessarily be available from an approach using a shuffle algorithm.

NOTE: some folks expressed concerns about how long this might take but it runs pretty quick, on my 2011 Macbook Pro it took a about a second for an array size of 1000. I didn't do a big-O analysis as a function of the array size, but that would be interesting too.

NOTE 2: its more elegant to use recursion for the numberInSet() function but it seemed best to keep simple.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h> /* If C99 */

const int ARR_SIZE = 1000;

/* Check if the number is in the set up to the given position: */
bool numberInSet(int number, int* theSet, int position);

int main()
{
    int* arr = malloc(sizeof(int)*ARR_SIZE);
    srand((unsigned int)time(NULL));

    /* Intialize array with rand entries, possibly duplicates: */
    for(int i = 0; i < ARR_SIZE; i++)
        arr[i] = rand() % ARR_SIZE;

    /* Scan the array, look for duplicate values, replace if needed: */
    for(int i = 0; i < ARR_SIZE; i++) {
        int loop = 0;
        while ( numberInSet(arr[i], arr, i-1) ) {
            arr[i] = rand() % ARR_SIZE;
            loop++;
        }
        /* could save the loop values here, e.g., loopVals[i] = loop; */
    }

    for(int i = 0; i < ARR_SIZE; i++)
        printf("i = %d, %d\n",i,arr[i]);

    /* Free the heap memory */
    free(arr);
}

bool numberInSet(int number, int* theSet, int position) {
    if (position < 0)
        return false;
    for(int i = 0; i <= position; i++)
        if (number == theSet[i])
            return true;
    return false;
}
Bruce Dean
  • 2,798
  • 2
  • 18
  • 30
-1

To make sure all random number you get in the same program are different, you must seed once the random generator:

srand (time(NULL)); //seed the random generator

//in the loop, rand will use the seeded value
rand() % 1000
rmonjo
  • 2,675
  • 5
  • 30
  • 37
  • 1
    This is not about random numbers, it's an ordered array but in random order. Totally different. – Jongware Dec 29 '13 at 17:28
  • not sure you read the question correctly + "it's an ordered array but in random order" doesn't mean anything. Please remove the unjustified -1 – rmonjo Dec 29 '13 at 17:33
  • "an array size 1000 where the numbers should be between 0-999. These numbers should be determined randomly (rand loop) and the number must not be repeated" -- the contents are **not** random, they are 0..999. – Jongware Dec 29 '13 at 17:39
  • hence the **% 1000** on the `rand()` that will keep random number between 0 - 999 – rmonjo Dec 29 '13 at 17:43
  • @rmonjo If there are 1000 elements each having a unique value in the range [0, 999], then no values are repeated; each integer in the range [0, 999] will be used exactly once. However, they will appear in a random order. The question isn't the usual "I generated X numbers randomly and they're all the same." –  Dec 29 '13 at 18:47