jeudi 25 juillet 2019

Is there a faster way, to get random numbers, from a specific pool of numbers

im looking for a faster method, which allows me, to get a random number from a certain pool of numbers which are stored in an array.

I'll need the method multiple times, and the current method is terribly slowing down the code.

#include <cstdlib>
#include <ctime>

int main()
{      
    const int size = 8;
    int numberArray[size] = { 0, 0, 3, 4, 0, 6, 7, 0 };
    srand(time(0));

    int rndIndex;
    int rndNumber;
    do 
    {
        rndIndex = rand() % size;
        rndNumber = numberArray[rndIndex];
    } while (rndNumber <= 0);
}

I like to get a random number from the array, except the random number is less than 0




Aucun commentaire:

Enregistrer un commentaire