Shuffle an array.

Quick way to shuffle an array.

Fisher–Yates shuffle

See version :

Pas de dépendances

Download :

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

void Melanger(int* tab,int nb)
{
    int i,nb2;
    nb2 = nb;
    for(i=0;i<nb;i++)
    {
        int tmp;
        int index = rand()%nb2;
        tmp = tab[index];
        tab[index] = tab[nb2-1];
        tab[nb-i-1] = tmp;
        nb2--;
    }
}

void Remplir(int* tab,int taille)
{
    int i;
    for(i=0;i<taille;i++)
        tab[i] = i/4;
}

void Afficher(int tab[],int taille)
{
    int i;
    for(i=0;i<taille;i++)
        printf("%d\t",tab[i]);
}


int main()
{
    int taille = 100;
    int* tab = malloc(taille*sizeof(int));
    srand(time(NULL));
    Remplir(tab,taille);
    Melanger(tab,taille);
    Afficher(tab,taille);
    free(tab);
    return 0;
}



Explanations

	No explanations yet.