Hello everyone, I have a project that includes random generator for a die. We are required to do it in this format:
When the interrupt is received from RB0/INT (push button), we will read the value of the TMR0 register and compute a random number as follows:
The first two least significant bits (bit 0 and 1), will generate a number between 0 (00 in binary) and 3 (11 in binary). This number will be added to one of the following three possibilities: 1, 2, or 3. This addition should also be randomly selected by reading the third and the fourth (bit 2 and 3) bits of the TMR0. Now, after the addition, we will get a number between 1 and 6.
Now, we still have to know when we have to add 1, 2, or 3. We will take the third and the
forth (bit 2 and 3) bits of TMR0 as a random selection of the value to be added. The
following table shows the different possibilities.
TMR0 (bit 2, 3) ADD
00 1
01 2
10 or 11 3
Example: If the TMR0 was 00001011, the random number will be
3 (bit 0 and 1 are 11) + 3 (bit 2 and 3 are 10) = 6.
When the interrupt is received from RB0/INT (push button), we will read the value of the TMR0 register and compute a random number as follows:
The first two least significant bits (bit 0 and 1), will generate a number between 0 (00 in binary) and 3 (11 in binary). This number will be added to one of the following three possibilities: 1, 2, or 3. This addition should also be randomly selected by reading the third and the fourth (bit 2 and 3) bits of the TMR0. Now, after the addition, we will get a number between 1 and 6.
Now, we still have to know when we have to add 1, 2, or 3. We will take the third and the
forth (bit 2 and 3) bits of TMR0 as a random selection of the value to be added. The
following table shows the different possibilities.
TMR0 (bit 2, 3) ADD
00 1
01 2
10 or 11 3
Example: If the TMR0 was 00001011, the random number will be
3 (bit 0 and 1 are 11) + 3 (bit 2 and 3 are 10) = 6.