Sunday, May 10, 2009

Non repeating random number generator ( C programming)?

I've been working on non repeating random number generator... and it doesnt work.. First, it doesnt generate "random" numbers every time I run it.. and second, some numbers repeat several times.





int main()


{


int array[16] = {0};


int x;


int y;





for(x = 1; x %26lt; 17; x++)


{


array[x] = rand()%16+1;





for(y =1; y %26lt; 17; y++)


{


if(array[y] == array[x])


{


if(y != x)


{


rand()%16 +1 == array[x];


}


}


}


if(array[x] == 16)


array[x] == ' ';


printf("%3d", array[x]);





if(x%3 ==0)


printf("\n");


}


system("PAUSE");


return 0;


}





Can someone tell me what is wrong with my program? Thanks!

Non repeating random number generator ( C programming)?
According to the documentation: The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.





Also please note when declaring the following:


int array[16] = {0};


you are making space for 16 int elements ie 0-15 so writing to array[16] is a problem, you may only write from array[0]-array[15]. Ive rewritten your for loops accordingly.








#include %26lt;time.h%26gt; // we are using time()





int main()


{


int array[16] = {0};


int x;


int y;





/* Seed the random-number generator with current time so that


* the numbers will be different every time we run.


*/


srand( (unsigned)time(NULL) );





for (x = 0; x %26lt; 16; x++)


{


array[x] = rand()%16+1;





/*


No need for this as you are already


filling the array correctly now





for(y=0; y %26lt; 16; y++)


{


if(array[y] == array[x])


{


if(y != x)


{


rand()%16 +1 == array[x];


}


}


}


*/


// This is wrong, the space character ' ', shouldnt be used


// in an int array and you are replacing any part of the


// array that equals 16 which isnt correct.


//if(array[x] == 16)


// array[x] == ' ';





printf("%3d", array[x]);





// I added x+1 so that the x=0 value is printed in the


// first row of 3


if((x+1)%3 == 0)


printf("\n");


}





system("PAUSE");


return 0;


}
Reply:i dont understand c yet, still in high school im getting there :), but are you resetting the for loop after a test fails?





for example if array(blah blah blah) = array(blah blah blah)





x = 1
Reply:Define an array of 16 elements with the numbers 1 to 16.


In a for loop of 8 times


get a random number between 0 to 15 // array subscript


do while (array[rnd]=0)


get a random number between 0 to 15 // array subscript


end do


Print array[rnd]


array[rnd]=0


end for





for i 0 to %26lt;16


if array[i] %26gt; 0


print array[i]


end-if


end for





If you make the first loop do all the work and do not use the second loop then the program will be quite slow getting the last few numbers.


by having the second loop the program will be very quick. You can play about with the number of times round the first loop.





Have fun.
Reply:dunno about c, but with VB you need to say "randomize" otherwise every time you run it it'll be the same





just my two cents
Reply:OK, first off, you want to seed the random number generator with a call like so:





srand( (unsigned)time( NULL ) ); //crucial!





2nd, I like to use a macro for making random numbers...i forget who gave me this, but thanx, you!





#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))





//...you might use it like so:


BOOL roll_dice_is_snake_eyes()


{


int d1,d2;





d1=GetRandom( 1, 6 );


d2=GetRandom( 1, 6 );


if(d1 == 1 %26amp;%26amp; d2 == 1)


return 1; //dice are snake eyes!


return 0; // not....


}





OK, 3rd, please know some of the science on this: Namely, just because some numbers repeat several times...that doesn't mean the code is failing.





For example, By definition, for 100 tries of random integers in the range of 1 thru 10, you *should* see each integer repeated 10 times (an even distribution) ...if the generator is _truly_ random.





If your generator must not repeat, then each time a new number is generated, store it in an array. Parse the array to ensure it wasn't already used before handing it to the user.








int array[20];





int used_already(int num)


{


int ii=20;


while(ii--)


if(array[ii] == num)


return 1;


return 0;


}





int store_it(int num, int count)


{


array[count++]=num;


return count;


}





int main()


{


int num, count = 0;





while(count %26lt; 20)


{


num =GetRandom(1,19);


if( ! used_already( num) )


{


count = store_it ( num, count );


printf("Hey! Good one: %d\n", num);


}


}


}





cheers!


No comments:

Post a Comment