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 Generator in C for 16F690

Status
Not open for further replies.

hitechbob

New Member
Hey

I'm trying to write a small ramdon number generator in C for the 16F690.

The numbers need to be in a specific range (say 1 -10).

Can anyone help me with this?

Thanks
Bob
 
https://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html

The srand() function allows you to set the seed (where the number chain starts from, this can be used to repeat a number chain) it's often fend some random data like the timer value when the key is pressed to generate the number.
the rand() function returns the next value in the chain from the given seed.

This has the range of an int, scaling is simply a matter of dividing.

If you're trying to actually write your own rather than use the C library. Look up linear shift register, this is the method that the stdlib rand function uses. I wouldn't bother rewriting the code since it's already there in the standard libraries. Anything more advanced than that will require basic understanding of cryptography as cryptography and random number generation go hand in hand.
 
Last edited:
The link I provided contains that information, please actually read and think about the posts provided. The srand function itself only seeds the rand results. Again please actually READ the link I provided. We're not here to hold your hand, you must show effort of your own.
 
It is really hard to get a small number set to look random without human intervention per Sceadwian's button/timer suggestion. Changing the random seed, mostly just shifts the same, or familiar bit patterns, left or right. My suggestion would be to find a couple of different seeds that look good on inspection, or provide the most different pattern from each other, and toggle between them.

Another suggestion would be to add a couple of "wild card" numbers, or bit positions, like 3 and 6, or whatever. Conditional testing the wild cards then call a procedure to loop through a another set of numbers upon each increment.
 
If a simple linear shift register doesn't suit your requirements.
**broken link removed**
Goes over in some greater details the jist of what I was talking about and mentions that AES can be used as a very non repeating random number generator. AES is one of the easiest to implement encryption algorythms to implement processor wise and can be done on a micro controller, though probably not very fast.
 
Well I guess the rand() function would work pretty well if the guidelines were 1-10, or better yet, 0-9 outcomes. Then it is just a matter of parsing out the %10, and say ignoring the leading zero's. And if that is what was meant, then my mistake, didn't occur to me at the time.
 
Here is some C code for a decent enough random number generator, the RNG method is taken from Microchip's appnote AN544 and converted by me to C.

Code:
//=============================================================================
//   MAKE RANDOM BYTE
//=============================================================================
void make_random_byte(void)
{
        //-----------------------------------------------------
        // this function make 8 new psuedo random bits in randL.
	// 16bit random number generator is taken from Microchip's
	// AN544 math routines appnote "psuedo random number generator";
	//  1. XOR bits 15^14 into A
	//  2. XOR bits 12^3 into B
	//  3. XOR bits A^B into C
	//  4. left shift rand var 1 bit
	//  5. put C in bit 0
	//-------------------------------------------------
        unsigned char rng_bitcount;	
        unsigned char rand_temp;    

	// generate 8 new RNG bits (in randL);
	rng_bitcount = 8;
	while(rng_bitcount)
	{
		// 1. XOR bits 15^14 into A
		rand_temp = 0;
		if(randH.F7 != randH.F6) rand_temp.F2 = 1;

		// 2. XOR bits 12^3 into B
		if(randH.F4 != randL.F3) rand_temp.F1 = 1;

		// 2. XOR bits A^B into C
		if(rand_temp.F2 != rand_temp.F1) rand_temp.F0 = 1;

		// 4. left shift rand var 1bit
		asm CLRF STATUS			;
		asm RLF randL,f			;
		asm RLF randH,f			;

		// 5. put C in bit 0
		if(rand_temp.F0) randL++;

		rng_bitcount--;
	}
}
//-----------------------------------------------------------------------------

It gives you a new random byte each time the function make_random_byte() is run.

To convert that new byte to a range 0-9 you just do a modulus 10 on it;

Code:
rando = (randL % 10) ;   // result in rando is now 0-9 range

(edit) I forgot to add, you need to start with a NON zero value in randH and randL, something like;
// put start seed in RNG when program starts
randH = 0x14;
randL = 0x37;
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top