1 digit random C code

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

As an IAR C Compiler user I can't type my question in GCC forum and I am really not sure which is the right forum to ask it. So, if I am wrong please forgive my mistake.

I am searching for a very small size pseudo-random function in C to produce 1 digit numbers from 1 to 10 (NOT 0). I don't care if there are duplicates.

Also, if there is any other smaller (in code size) solution that produces 1 digit numbers from 1 to 8 it could fit to my needs.

Could you please help me?

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

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

rand() % 10 should work?

Imagecraft compiler user

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

The other option is to roll Your own polynomial based 8 bit random number generator.

EDIT: how long an epoch do You require?

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

Two articles in Tutorial about generating random numbers:

https://www.avrfreaks.net/index.p...
https://www.avrfreaks.net/index.p...

But note that they are mainly trying to offer "better" than rand()/rnd() so maybe aren't optimized for code size.

BTW assuming this is for use on an 8bit AVR I guess "AVR Forum" is actually the most appropriate place for the question in fact.

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

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=95614&highlight=radbrad
I remember this being posted - it's in assembly, rather than C, but it's so small I'm sure some kind soul can convert it for you.

You are fairly unlikely to get anything smaller.

Four legs good, two legs bad, three legs stable.

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

I was thinking that a good binary random number would result by toggling the var in a tight loop while a button is pushed, when released, its in whatever state it is in. If you can toggle the var in about 4 cycles, it will toggle 64 million times per second. I cant quite time the release that fine. Counting 0-9 would 'slow it down' to only 6 million dice rolls per second.

Imagecraft compiler user

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

The 'coolest' RNG I've seen recently was to utilise a very noisy linear regulator, especially wired to create more noise, and feed the very noisy signal into an ADC :)

See..

Quote:
LM2931-5.0 is a Random Noise Generator too
By Petre Tzvetanov Petrov (Bulgaria)
in Elektor July/August 2011

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

Thank you all guys.

You have been very helpfull.

Regars,

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

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

If you really want an 8bit generator (and a look-up table is to large) this one is the only one I've seen on this site with descent random properties.

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

Thank you snigelen.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

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

To tell you the truth I finally did this (just to reduce the code size - and it seem to be very good for my needs):

In my application I have an interval timer run in 31.25KHz and it is used to produce an 8bit PWM at OC0A pin.

my main loop has:

void main(void){
system_init();

while(1){
__watchdog_reset();
scenario();
}
}

The scenario() function is a part of code that includes a lot of processes. I have found that it is not periodically due to diference from execution to execution. So inside the scenario() function I asychronously call the function random = get_random():

void scenario(void){
// code
// code
// code
random = get_random();
// code
// code
// code
}

unsigned char get_random(void){
return (TCNT0 & 0x07) + 1;
}

So I get a number from 1 to 8. Putting the results in an excel sheet I realy see no period.

I think that it is something clever like bob's suggestion about the use of a tacktile button.

What do you think?

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

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

Quote:

What do you think?

For randomness you need "entropy". In Windows and Linux systems entropy is injected into their random number systems by completely unpredictable events like button presses (timing between), mouse movements, arrival of Ethernet packets at the Mac-Phy, etc. So do you have any other potential sources of entropy in your system? That is external events occurring at unpredictable times for which the number and timing of events could be used to see a PRNG.