Announcement

Collapse
No announcement yet.

Need a random number generator fix.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need a random number generator fix.

    Hello,

    I have been working hours with the random number generator which seems to not be random at all. I need someones help here please. Does anyone have any code they are willing to share showing how to make the random number generator actually random without adding additional external hardware. I am using an external 9v AA battery pack for power and want to generate a random number on every power cycle. Running only a uOLED-128-G2 display, Goldelox.

    At every power up it seems that the random number is always the same.

    "pick" is set as a var pick := 0;

    Thanks,
    Rob

    SEED(1000); // set a number range to randomly choose from

    for (cyc:=0; cyc<3; cyc++)
    pick := ABS(RAND() % 50); // picks 0 to 50
    print(pick); // to debug
    pause(300); // to debug
    gfx_Cls();
    next

    print ("Random ", pick); // random picked #
    pause(1000);
    gfx_Cls();
    Last edited by Rob A; 19th December 2018, 05:01 AM.

  • #2
    I'm not feeling any love here. No support, no help and no responses. Chose the wrong company?

    Comment


    • #3
      Sorry, somehow this slipped through the cracks.

      At best, most computers can only generate pseudo random numbers. So using a fixed seed, and then the third pseudo random number will always give the same number (after a reset/power cycle).

      Computers need a 'randomish' number to pick the seed to use (eg RANDOMISE, where it exists). This is normally accomplished using the date/time and/or the 'clock units' since startup to set the 'seed'.

      GOLDELOX does not have a clock, and since it is not multitasking the 'clock units' (ie. SYSTEM_TIMER_LO ) will (nearly) always be the same if you set that to the SEED.

      So we need another way, we need to get a random, or pseudo random input from an external source.

      If you have low voltage AC nearby, one way is to condition it to be 0-3.3v, generally by 1/2 wave rectification and ranging it from 0 to 3.3v using a resistor divider and a zener to limit it to 3.3v. You can then use the analogue value as the seed. You could also wait for a digital transition and then use SYSTEM_TIMER_LO at that point.

      Failing that you could add a wire an analogue input to pick up 'noise', then condition it and use it as above. But beware that this might be hard to do 'reliably', so plenty of testing will be required.

      All this gets a bit easier on systems with touch input as you can 'just' SYSTEM_TIMER_LO as the seed at the time of the first touch.
      Mark

      Comment


      • #4
        Hi Mark,
        Working on an external single chip solution. Maybe switch the analog IO1 between my vbat voltage check and a PIC or 1- wire Maxim device. This product Goldelox should have had an internal solution or at least a second analog input being a potential battery device. Maybe G3 version of the die. Thanks for the reply! Other posted questions yet waiting replys.
        Last edited by Rob A; 22nd December 2018, 12:56 AM.

        Comment

        Working...
        X