Pergunta

I want to write a program so that i print out 10 random numbers every time i run it, the random numbers printing out should be 1-10, also they should never repeat.

UPDATE:Sorry for not stating the exact problem, basically the while loop that is suppose to re assign the random numbers only if it hasent been used is causing my program not to print anything at all. If i comment out the entire while loop and leave the printf at the bottom it prints out 10 random numbers between 1-10, however it just prints out repeats.

Could someone tell me how i can fix my code or give me some tips?

#include <stdio.h>
#include <time.h>

int main()
{
int array[10];
int x, p;
int count;
int i=0;

srand(time(NULL));

for(count=0;count<10;count++){
array[count]=rand()%10+1;
}

while(i<10){
int r=rand()%10+1;

for (x = 0; x < i; x++)
{
if(array[x]==r){
    break;
}
if(x==i){
    array[i++]=r;
}
}

}
for(p=0;p<10;p++){
printf("%d ", array[p]);
}
return 0;
}
Foi útil?

Solução 2

if(x==i) can never be true inside the for (x = 0; x < i; x++) loop, therefore the while loop does never terminate. The if statement has to be moved after the for-loop:

while(i<10){
    int r=rand()%10+1;

    for (x = 0; x < i; x++)
    {
        if(array[x]==r){
            break;
        }
    }
    if(x==i){
        array[i++]=r;
    }
}

Also the first loop

for(count=0;count<10;count++){
    array[count]=rand()%10+1;
}

is unnecessary because the values are overwritten later.

(If you look-up "random permutation" or "Fisher–Yates shuffle" then you will find more efficient algorithms for producing a non-repeating sequence of random numbers.)

Outras dicas

Using a shifting mask algorithm, you can generate pseudo-random numbers, non-repeating. I've included one of my functions giving an example of this, below. This process is 10x faster than any other offered algorithm, also uses no extra memory. This kind of algorithm is typically used for "digital dissolve" and "scatter" effects, etc., however my implementation focuses on a single dimensional effect.

Enjoy, Dr. B

/* Bryan Wilcutt's random scatter algorithm */
/* Generates random-appearing, non-repeating values. */

/* Can be any number within the given range of 32K 
   Mask must be changed for other ranges.  */

#define START_POINT 1

void randScatter()
{
    long mask;  /* XOR Mask */
    unsigned long point;
    int val;
    unsigned int range = 0x7fff; /* 32K */

    mask = 0x6000; /* Range for 32K numbers */

    /* Now cycle through all sequence elements. */

    point = START_POINT;

    do {
        val = point % range;    /* Get random-appearing value */
        printf("%08x\n", val);

        /* Compute the next value */

        if (point & 1) {
            /* Shift if low bit is set. */

            point = (point >> 1) ^ mask;
        } else {
            /* XOR if low bit is not set */

            point = (point >> 1);
        }
    }  while (point != START_POINT); /* loop until we've completed cycle */
}

(1) Your indentation is wrong. The right one would be:

#include <stdio.h>
#include <time.h>

int main()
{
    int array[10];
    int x, p;
    int count;
    int i=0;

    srand(time(NULL));

    for(count=0;count<10;count++){
        array[count]=rand()%10+1;
    }

    while(i<10){
        int r=rand()%10+1;

        for (x = 0; x < i; x++)
        {
            if(array[x]==r){
                break;
            }
            if(x==i){
                array[i++]=r;
            }
        }
    }
    for(p=0;p<10;p++){
        printf("%d ", array[p]);
    }
    return 0;
}

(2) Once the indentation is correct, it is easier to see the problem. The if(x==i) part should be inside the while but outside the for.

#include <stdio.h>
#include <time.h>

int main()
{
    int array[10];
    int x, p;
    int count;
    int i=0;

    srand(time(NULL));

    for(count=0;count<10;count++){
        array[count]=rand()%10+1;
    }

    while(i<10){
        int r=rand()%10+1;

        for (x = 0; x < i; x++)
        {
            if(array[x]==r){
                break;
            }
        }
        if(x==i){
            array[i++]=r;
        }
    }
    for(p=0;p<10;p++){
        printf("%d ", array[p]);
    }
    return 0;
}

This prints the correct result.

(3) You can now remove the for(count=0;count<10;count++) part which fills the array with repetitions. Its result is overwritten by the other part.


A moralizing note: proper formatting actually helps spot the error. Don't neglect it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top