| ??? 12/30/03 10:07 Read: times |
#61564 - RE: Random number from 1 to 8 Responding to: ???'s previous message |
I'm going to try this again.
First a comment. Often cases where a "random" presentation is given to a user/operator/player it is not a good idea to present repeat cases at all. This is especially true in situations where the number of possible options is just a few. In your case of 1 to 8 choices it is a few. It would be all different if the number of cases was 100 or 256...but 8 is a few. So I modified the Keil C51 program to as shown below so that it eliminates repeat cases.
#include <stdlib.h>
unsigned char xdata array[300];
void main(void)
{
int r;
unsigned char rr;
unsigned char lrr=0;
int i;
for(i=0; i<300; i++)
{
do
{
r = rand();
rr = ((r / 7) % 8) + 1;
}
while(rr == lrr);
lrr = rr;
array[i] = rr;
}
while(1)
{
}
}
The chart of the corresponding mapping is shown below for the case of the default zero for the rand() function seed.
If the list of 300 "random" non-repeating selections is sorted and plotted on a similar graph it is possible to see the relative distribution of selections at each value. See below:
Michael Karas |



