Nombre aléatoires c++

xoxo91 Messages postés 38 Date d'inscription   Statut Membre Dernière intervention   -  
.N Messages postés 25 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour Tout le monde,


je voudrai savoir comment faire pour génèrer par exemple une serie de 10 combinaisons par hazard mais sans répétitions, sur c++ ou csharp ça me cause aucun probléme , je voudrai juste savoir la logique pour qu'on puisse faire ça .

Mercii d'avancee.
A voir également:

1 réponse

.N Messages postés 25 Date d'inscription   Statut Membre Dernière intervention   570
 
Bonjour. En fait, pour pouvoir générer des nombres aléatoires sans répétition, il faut décrémenter l'espace de génération aléatoire (le «range») de 1 (ou si tu préfères, soustraire 1 élément à l'ensemble fini de départ et d'arrivé) à chaque itération. Cela implique bien sûr que le nombre d'éléments générés doit être inférieur à celui-ci, puisqu'il y aurait forcément répétition. À chaque «rand()», on doit incrémenter le nombre généré pour chaque éléments inférieur ou égal à celui-ci. On obtient à la fin un tableau («array») de nombre aléatoire donc aucun n'a la même valeur.

Voici un exemple (tu peux copier les fonctions pour les utiliser dans tes codes, si tu veux) :
#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 

#define USING "a.out <elements count> <lower born> <upper born>\n" 
#define COUNT_ERROR "Error : count must be greater than zero.\n" 
#define BORN_ERROR "Error : the lower born must be lesser than the upper born.\n" 
#define RANGE_ERROR "Error : the elements count must be lesser than the rand range.\n" 

unsigned long ulong_rnd() 
{ 
    unsigned char bytes[4] = {rand(), rand(), rand(), rand()}; 

    unsigned long returnValue = 0; 
    returnValue  |= bytes[0] << 24; 
    returnValue  |= bytes[1] << 16; 
    returnValue  |= bytes[2] << 8; 
    returnValue  |= bytes[3] << 0; 

    return returnValue; 
} 

int ulong_rnd_fill(unsigned long *pOut, long count, unsigned long min, unsigned long max) 
{ 
    if (max < min) return -1; 

    unsigned long mod = max - min + 1, lngtmp; 

    if (mod < count) return -2; 

    unsigned long *pSort = (unsigned long*) malloc(sizeof(unsigned long) * count); 

    if (!pSort) return -4; 

    long i, ii; 
    for (i=0; i<count; i++) 
    { 
        pSort[i] = ulong_rnd() % mod + min; 
        for (ii=0; pSort[i]>=pSort[ii] && ii<i; ii++, pSort[i]++); 
        pOut[i] = pSort[i]; 
        for (;ii<i; ii++) 
        { 
            lngtmp = pSort[ii]; 
            pSort[ii] = pSort[i]; 
            pSort[i] = lngtmp; 
        } 

        mod --; 
    } 

    return 0; 
} 

void printArrayLng(long *pIn, int size, const char* array_name) 
{ 
    int i; 
    for(i=0; i<size; i++) printf("%s[%d] = %ld\n", array_name, i, pIn[i]);; 
} 
int main(int argc, char *argv[]) 
{ 
    if (argc != 4) 
    { 
        printf(USING); 
        return -1; 
    } 

    long elem_count = atol(argv[1]), 
         lowerBorn  = atol(argv[2]), 
         upperBorn  = atol(argv[3]); 
    if (elem_count < 0) 
    { 
        printf(COUNT_ERROR); 
        return -1; 
    } 

    if (lowerBorn > upperBorn) 
    { 
        printf(BORN_ERROR); 
        return -1; 
    } 

    if (upperBorn - lowerBorn + 1 < elem_count) 
    { 
        printf(RANGE_ERROR); 
        return -1; 
    } 

    srand(time(NULL)); 
    long *rnd_array = (long*) malloc(sizeof(unsigned long) * elem_count); 

    ulong_rnd_fill((unsigned long*)rnd_array, elem_count, 0, upperBorn - lowerBorn); 

    long i; 
    for (i=0; i<elem_count; i++) rnd_array[i] = ((unsigned long)rnd_array[i]) + lowerBorn; 
    printArrayLng(rnd_array, elem_count, "random"); 

    return 0; 
} 

0