Tako Kichi
New Member
Hi,
I am working on my first PIC project using a 16F818 and have hit a math problem that currently has me stumped. I am writing the program in PIC Basic but can insert assembly code blocks if required. My problem is as follows:
I need to generate a pseudo random number that falls between 1 and 60. I know how to generate the random number (there is a RANDOM command available) and at the moment I have stored this in location 'W1'. However, the number can be between 1 and 65,535 and this is where my math skills are letting me down. I need a way of reducing that random number until it falls into the 1-60 range so that I can then use that figure to generate a delay in seconds based on the new number.
I had thought about trying to 'sample' the last 7 bits of the number and just using those (the idea was that it would be between 1 and 63 which is close enough) but cannot find a way to do that.
I have also thought about dividing by both 32 and 100 to try to 'shift' the number into the required range but again I have had no luck as I do not know what the number is and therefore how many times I would need to go around a loop to reduce it.
Does anyone know of a method where I can either limit the random number to the 1-60 range or a method of reducing some unknown number until it falls into that range?
Any help will be gratefully received as I have now come to a complete stop on this project until I can get this sorted.
Thanks,
Larry
I am working on my first PIC project using a 16F818 and have hit a math problem that currently has me stumped. I am writing the program in PIC Basic but can insert assembly code blocks if required. My problem is as follows:
I need to generate a pseudo random number that falls between 1 and 60. I know how to generate the random number (there is a RANDOM command available) and at the moment I have stored this in location 'W1'. However, the number can be between 1 and 65,535 and this is where my math skills are letting me down. I need a way of reducing that random number until it falls into the 1-60 range so that I can then use that figure to generate a delay in seconds based on the new number.
I had thought about trying to 'sample' the last 7 bits of the number and just using those (the idea was that it would be between 1 and 63 which is close enough) but cannot find a way to do that.
I have also thought about dividing by both 32 and 100 to try to 'shift' the number into the required range but again I have had no luck as I do not know what the number is and therefore how many times I would need to go around a loop to reduce it.
Does anyone know of a method where I can either limit the random number to the 1-60 range or a method of reducing some unknown number until it falls into that range?
Any help will be gratefully received as I have now come to a complete stop on this project until I can get this sorted.
Thanks,
Larry