how to produce a 6 bit random integer
how to produce a 6 bit random integer. i means how to control the bits
e.g.
213432,
312356,
,,,,,
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
main()
{
int i;
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
printf("new line \n");
}
Just to clarify s. roelants' answer
Problem with rand() on some machines is that the range is from 0 to RAND_MAX. On a lot of compilers, this happens to be 32767. What is being done in the solution is to get two pseudo random 3 digit numbers and concatenate them.
concerning random character of concats
Solarflare is absolutely correct. Since RAND_MAX is not an exponent of ten (on any machine I've seen, but generally its at least not guaranteed to be), there will be some numbers that will have a greater likelihood of occuring than others.
For example: take RAND_MAX = 32767
taking % 1000 gives
33 possibilities to get #s 0 - 767
32 possibilities to get #s 768 - 999
but taking % 10 gives
3277 posibilities to get #s 0 - 7
3276 possibilities to get #s 8, 9
By choosing a smaller modulus to concatenate makes the likelihood ratios closer to 1:1, thus giving a better random character. Of course, the best way is to just exclude the numbers from RAND_MAX down to RAND_MAX - RAND_MAX % N (N = 10, 1000, etc) as contributing to the asymmetry by taking another rand if such occurs (repeat as necessary). Then, its a tradeoff between rand returns to exclude and number of calls of rand to concatenate...