Edgar
Edgar

Reputation: 3

how to shuffle random number in array with a given range in C

i've looked up this topic everywhere. i havent found any topic with my condition. i want to generate array[10] with random number from -100 to 100. this is my code

int main (){
int a[10];
int i;

for (i=0;i<10;i++)
{
    a[i]= rand() % 201 -100;
    printf ("%d\n",a[i]);
    }


    system ("pause");
    return 0;
}

the output is same everytime i run the program.. my question is how to shuffle those random number array ? im sorry for my bad english

Upvotes: 0

Views: 640

Answers (4)

MisterEd
MisterEd

Reputation: 1

Here's working code that takes input of a low and high number from the user, generates an array, prints the array in order and then shuffles using a Fisher-Yates inspired shuffle with rand() and prints the shuffled array. It should work for nearly any range of numbers and could be used to shuffle characters as well with some minor modifications.

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

int main(void)
{
    int low, high;
    printf("Choose a range of numbers and first enter the lowest number in the range: ");
    scanf("%d", &low);
    printf("Please enter the highest number in the range: ");
    scanf("%d", &high);
    int arraylength = high - low + 1;
    int numarray[arraylength];

    printf("Here is your range of numbers in numerical order:\n");

    /* Create array from low and high numbers provided. */
    int i;
    int j = low;
    for (i = 0; i < arraylength; i++)
    {
       numarray[i] = j + i;
       printf("%d\t", numarray[i]);
    }

    /* Shuffle array. */
    srand(time (NULL));
    int temp;
    int randindex;
    for (i = 0; i < arraylength; i++)
    {
        temp = numarray[i];
        randindex = rand() % arraylength;
        if (numarray[i] == numarray[randindex])
        {
            i = i - 1;
        }
        else
        {
            numarray[i] = numarray[randindex];
            numarray[randindex] = temp;
        }
    }

    printf("\nHere is your range of numbers in random order:\n");

    /* Print shuffled array. */
    for (i = 0; i < arraylength; i++)
    {
        printf("%d\t", numarray[i]);
    }
    getchar();
    return 0;
}

Upvotes: 0

unwind
unwind

Reputation: 399793

You don't need to shuffle, it's easier to seed the random generator.

In C, this is typically done by calling srand() with the current time, like so:

srand(time(NULL));

Note that there is no logical connection between the random number generator and the time of day; it's just a convenient way of getting a number that is reasonably likely to be different every time you run the program. You can also seed the generator from some different source, if you have one handy.

Also note that your array of random numbers can contain duplicates, unless you add code to filter them out which can be ... annoying.

Upvotes: 4

Leo Chapiro
Leo Chapiro

Reputation: 13984

You need to initialize the random seed to a value representing the current time (calling time) to generate a different value:

/* initialize random seed: */
  srand (time(NULL));

Upvotes: 3

Ivan Bacher
Ivan Bacher

Reputation: 6154

See This

Hope it helps

Otherwise you could try seeding the rand with the current time.

Upvotes: 0

Related Questions