How many LEDs do you have? 8?
just start with a number, do something to it like multiply and divide it, add or subtract numbers, use a longish number then just use the last decimal digit or do it all in binary and you can just use the last 4 digits, light up the let that corresponds to the number then rerun the computation, you can add in the check that your not using the same number again.
The actual syntax will depend on the language your using
what will you supply the ADC with ?
I do have an random number using rand() but i think it is in binary and between 0b00000000 and 0b11111111 so 0 and 255 decimal. Any idea how to convert it to decimal?
Hello all,
For a project I need to set an random led high.
So what I would have to do is:
1. create a random number between 0 and 7
2. save that number in a variable
3. light the led
4. save same random number in different variable
5. generate new random number
6. compare old and new random number: if they are equal generate new random number.
run rest of code.
Yesterday in the shoutbox I got pretty far but just barely not there. I do have an random number using rand() but i think it is in binary and between 0b00000000 and 0b11111111 so 0 and 255 decimal. Any idea how to convert it to decimal? The function doesn't take arguments so the only thing I can come up with is to make the function recursive and let it run until it is within parameters. Timing does not matter. It doesn't have to be completely random. Pseudo random is good enough.
Than there is still the issue of setting and clearing the same led as number in the variable. You cant just use RD num = 1; so how would you do that?
I have something like it running in Python but there you have the great random.randrange()
EDIT: i am using the Hi Tech C compiler
#define BIT(word, n) ((word >> n) & 1)
#define randToDecimal(word) BIT(word,0) + BIT(word,1)*2 + BIT(word,2)*4
void main ()
{
int randomNumber = rand();
printf("Random number: %d\n", randomNumber);
printf("3 LSB to decimal:%d\n",randToDecimal(randomNumber));
}
unsigned char Value=0, oldValue=0 ;
for (;;)
{
Value = rand() & 0x07 ; // mask all most significant bits except the 3 LSB:
// 'Value' is between 0 and 7
if (Value != OldValue)
{
OldValue = Value ;
// light the 'nth' LED using the number (0-7) stored in 'Value'
}
}
If you need a seed for your random generator, the suggestion to use the Least Significant Bits (LSB) of an ADC channel is a good one.I think it is very simple:Code: [Select]unsigned char Value=0, oldValue=0 ;
If you need a seed for your random generator, the suggestion to use the Least Significant Bits (LSB) of an ADC channel is a good one.
for (;;)
{
Value = rand() & 0x07 ; // mask all most significant bits except the 3 LSB:
// 'Value' is between 0 and 7
if (Value != OldValue)
{
OldValue = Value ;
// light the 'nth' LED using the number (0-7) stored in 'Value'
}
}