Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Random number generate in assembly.

Status
Not open for further replies.

Peet19

Member
Hi!
I would like to generate a random number in assmebly from 0 to 50. I would like some help with this.
The PIC a 18F24K20.
Thank you in advance for your help!
 
Bear in mind all these type of things only create pseudo random numbers, you need to somehow generate a random seed to start with - if you start with the same seed, you get the exact same pseudo-random sequence.

A simple solution, depending on your application, is to use a timer to generate a 'random' seed number - as the program starts up start a fast timer counting, and display "press button to start" - when the button is pressed, read the counter value and use that as the random seed.
 
I looking, thanks.

Nigel: I understand, but there is no outside intervention. The program should automatically generate a random number at certain intervals.
 
Maybe this -


Thius is not ASM but you could use a compile to produce the ASM to incorporate
into your code.


Regards, Dana.
 
Last edited:
If you have a spare I/O, an external RC timer with 'R' being a photo sensor works well
That sounds a good idea - I was thinking along the lines of a resistor and electrolytic capacitor, on the grounds that the leakage and charging time of an electrolytic probably isn't all that accurate and repeatable, and by using a fast timer/counter to generate the seed a minor change of capacitor timing would produce a decent range of counter variation.

Presumably not a good idea to have the LDR inside a light proof box :D
 
I would try the ADC route... A spare input from a floating adc pin can produce random noise..
Leakage can drive the pin to one rail or the other.

But use A/D to look at its Vdd pin, typically thats 100+ mV of noise on it,
somewhat correlated but usable as a seed.


Regards, Dana.
 
Thanks everyone for your help. It's slowly coming together.
One more question.
I would like to copy the lower four bits of a byte to another register.
How do I do this?
 
Check out the Math Logic instructions,AND, XOR, IOR etc, in this case ANDLW or ANDWF If the lower four bits are a 1 when and'ed with the data to be copied, the lower four bits are copied over the high four are cleared, if this will do it for you.?
 
Thanks, but that's not much help to me. An example would be nice.
For now, I solved it by erasing the upper four bits one by one and copying the value of the register to WREG.
But that's not the real thing.
 
Long time since I did any pic assembly but it's something like,
Code:
    movfw    var            ;get variable
    andlw    0x0f           ;keep only bottom 4 bits
    movwf    lowerbits      ;store in new variable

Mike.
 
Hey, if it's on a microcontroller you could read a floating analog pin, cut off the first part that isn't varying, multiply the part that is varying by some appropriately sized number, modulo it, and use it as a seed.

You can't just use that AS the random number because it's likely a normal distribution about some value, not random.
 
If using a floating ADC pin then I'd just use the LSB and shift it into a variable until you have the number of bits you need. To get a 32 bit random seed would be very quick.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top