gcc func to give random number of 0-255

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm still pretty new to gcc and C in general (I stink at C)
Has anyone created a function that can generate pseudo random
numbers in the range of 0 to 255 ? if so I'd love to see the code :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Recently someone mentioned the follwing pointer to random number generators:
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=32107
but i think for you this is overshoot. Just read an unconnected ADC input pin of your chip (if available) and shift/mask/truncate the result.

-- Thilo

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That ``overkill'' solution is basically the implementation of the
librarie's rand() and random() functions. Just check the library
documentation on this.

Basically, in order to restrict the result to the range [0..255], it's
only needed to mask it with 0xff.

Of course, this is a PRNG (a pseudo-random number generator). Not
sure if this is what the OP really wanted. The suggested solution
with an ADC would result in true randomness (unpredictability of the
result), albeit usually only with very few bits. Thus the common way
is to use these few bits of true randomness as the seed value for the
PRNG, and then continue with a pseudo-random number sequence. If
needed, the PRNG can be re-seeded frequently.

If someone is interested and can read German (Thilo, I know you can
:), there has been a thread here:

http://www.mikrocontroller.net/forum/read-2-221331.html#222451

The URL points directly to the article with my test results,
indicating the histogram of result values. It's pretty clear the
results form a nice Gaussian distribution, so they are supposed to be
usable as a source of randomness.

In short, the setup looks like:

. resistor 1.8 kOhm from Vcc to PA3
. Z diode 3.3 V from PA3 to GND
. resistor 22 kOhm from PA3 to PA2
. capacitor 22 uF from PA2 to GND

The ADC has been run in diff mode with 20x amplification, so the noise
from the Z diode (as well as other noise coupled into PA3) would be
amplified enough to modify the ADC value. Obviously, the center of
the Gaussian curve is not 0, indicating the offset value of the
preamplifier.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The following code snippet is from a recent project of mine:

volatile unsigned int		count=0x8765;

unsigned char random(void)
{
	if((count&0x8000)==0)
		count=count<<1;
	else {
		count=count<<1;
		count = count ^ 0x1D87;
		} 
	return(count & 0xff);
}

Hope that helps :)

Tim Ressel
Portland, OR
timr@earthling.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there any reason to reinvent the wheel, and not use the librarie's
function?

After all, the algorithm used in the library implementation has been
verified by mathematicians to be good. I wouldn't give up that
qualification for a quick hack of myself.

Still, it's a PRNG, not a true random number.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the date/time is available in the system that makes a good seed.

Ken

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Spoo! (oops spelled backwards) I didn't realize there was a libc function for that!
Guess I better spend more time RTM :oops:

Tim Ressel
Portland, OR
timr@earthling.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> If the date/time is available in the system that makes a good seed.

Actually, it makes about the poorest seed you could think of
(immediately after not seeding the PRNG at all). The point is that
everything that is predictable is unsuitable as a seed in order to
turn a PRNG into a RNG. I'm sure the crypto guys have written
numerous explanations on that.

For true randomness, you need something that's completely
unpredictable from the outside (even to someone who knows the
algorithm). Not too many bits of randomness are required (that's
going to be done by the PRNG once it has been seeded), complete
unpredictability is more important. That's why I explained the
noise-based circuit above...

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.