Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:36 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Hardware generated random numbers

     Page 1 of 2    
Author Message
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 05:06pm 05 Feb 2023
Copy link to clipboard 
Print this post

Picomite - 5.07.06

Got talking in class about random numbers and how "most" sources of random numbers are not really truly random and this led to a search for how its done in hardware.  We looked at atmospheric detectors, Geiger counters, transistor in "avalanche breakdown" and oddly, lava lamps.

Anyway, this led to sampling floating ADC input and checking the LSB. LSB coded, 0-4 = 1 and 5-9 = 1. Then rejecting the data if the LSB from both samples is the same. (von Neuman "whitening" I think this is called).



'USING FLOATING INPUTS, SAMPLE X2 ADCS AND CHECK THE LSB OF EACH SAMPLE.
'IF LSBS ARE THE SAME - REJECT THEM. IF DIFFERENT, USE LSB1 (ARBITARY)
'0-4 = 0, 5-9 = 1

SAMPLES = 16
Dim FLOAT SAMP1(SAMPLES-1)
Dim FLOAT SAMP2(SAMPLES-1)
Dim BIT8(7)
NO_BITS=50 ' EACH RANDOM SEQUENCE HAS 50 BITS
NO_STREAMS = 10

ADC OPEN 1000,2

For Z = 0 To NO_STREAMS

BITSTREAM$=""

I=0

Do

ADC START SAMP1(), SAMP2()

'GRAB SAMPLES - 16 SHOULD BE ENOUGH
For Y = 0 To (SAMPLES)-1)
 YT1$ = Str$(SAMP1(Y))
 YT2$ = Str$(SAMP2(Y))

'EVALUATE THE LSB - SHOULD BE 12 DIGITS
'IF 12, SELECT IT.  IF LESS THAT 12
'IT WAS 0, SO SET TO ZERO.
'0-4 = 0, 5-9 = 1

 Select Case Len(YT1$)
Case 12
 LSB1=Val(Right$(YT1$,1))
 If LSB1 >4 Then
LSB1 = 1
 Else
LSB1 = 0
 EndIf
Case Else
 LSB1=0
 End Select

 Select Case Len(YT2$)
Case 12
 LSB2 = Val(Right$(YT2$,1))
 If LSB2 >4 Then
LSB2= 1
 Else
LSB2=0
 EndIf
Case Else
 LSB2=0
 End Select

'IF THE LSBS ARE THE SAME - DO NOTHING
'IF LSBS ARE DIFFERENT, USE THE LSB1
'INCREMENT THE COUNTER

 If LSB1 <>  LSB2 Then
Inc BITSTREAM$, Str$(LSB1)
I = I + 1
 EndIf

Next Y 'MOVE TO NEXT SAMPLES

'WE DONT KNOW THE EXACT LENGTH I FROM EACH LOOP

Loop Until I > NO_BITS 'STOP WHEN MORE THAN ENOUGH BITS

Print (Left$(BITSTREAM$,NO_BITS))  ' PRINT THE BITSTREAM

Next Z



Seems to work "well"  -- posting here for words of wisdom / suggestions and general insight into how I could do this better.

Would be interested into other "easy" to implement hardware solutions using the mite.  Currently playing with an AM radio tuned between stations and using that as the ADC source instead of leaving it to float -- will compare the two.

Cheers all
Nimue

-edited for grammar
Edited 2023-02-06 03:13 by Nimue
Entropy is not what it used to be
 
TimD
Newbie

Joined: 23/02/2021
Location: United Kingdom
Posts: 27
Posted: 05:22pm 05 Feb 2023
Copy link to clipboard 
Print this post

I did something similar years ago by just having several astable multivibrators, each running at high - but differing - frequencies, and each representing a bit of the random number to be read by the microcontroller.  So using just a hex Schmidtt inverter IC (like 74LS14), 6 capacitors & 6 resistors attached to 6 GPIO inputs, it's then very simple on the software side to combine the 6 binary inputs into a 6-bit number.  Not perfect obviously, but reasonable if your sampling is not too high frequency or regular.

- Tim
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 05:27pm 05 Feb 2023
Copy link to clipboard 
Print this post

Any of my programs that are supposed to give known results....  :(

It goes with "Amplifiers oscillate, oscillators don't". :(
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 05:34pm 05 Feb 2023
Copy link to clipboard 
Print this post

I seem to recall reading that one mechanism used on the ZX Spectrum for generating (non-)random numbers was simply to read bytes from a certain section of the ROM that was regarded as particularly "random". The time taken for the user's first input action to a program (keyboard, joystick, etc.) would determine which address in the ROM to start from.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 06:00pm 05 Feb 2023
Copy link to clipboard 
Print this post

  thwill said  I seem to recall reading that one mechanism used on the ZX Spectrum for generating (non-)random numbers was simply to read bytes from a certain section of the ROM that was regarded as particularly "random". The time taken for the user's first input action to a program (keyboard, joystick, etc.) would determine which address in the ROM to start from.

Best wishes,

Tom


That's interesting -- I wonder if the Hobbit used that as it had some odd interpretations of "random".

Cheers
N
Entropy is not what it used to be
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 06:50pm 05 Feb 2023
Copy link to clipboard 
Print this post

"Thorin sings about gold"

He may well have done, he drove me flippin' mad with his racket!
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 07:30pm 05 Feb 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  "Thorin sings about gold"


"Thorin sits down and starts singing about gold"

How to get a blank look from 99% of any audience.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Bill.b

Senior Member

Joined: 25/06/2011
Location: Australia
Posts: 235
Posted: 10:42pm 05 Feb 2023
Copy link to clipboard 
Print this post

Bach in the old days before controller chips I used a white noise generator fed to a logic gate to create a random movements of a robot car.

Bill
In the interests of the environment, this post has been constructed entirely from recycled electrons.
 
DrifterNL

Regular Member

Joined: 27/09/2018
Location: Netherlands
Posts: 58
Posted: 01:44am 07 Feb 2023
Copy link to clipboard 
Print this post

Something I thought of years ago was using the contact bounce that mechanical contacts have when switching to produce a random number.

https://www.instructables.com/4-Bit-True-Random-Number-Generator/

What you could try is having one port controlling a relay and connecting the contacts of the relay to a counting port.
Edited 2023-02-07 11:47 by DrifterNL
Floating Point Keeps Sinking Me!
Back To Integer So I Don't Get Injured.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 09:08am 07 Feb 2023
Copy link to clipboard 
Print this post

Noise diode->amplifier->shaper->divider = random pulses for you to time.
Feed into a 8-bit shift register and read in parallel every now and again.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 09:51am 07 Feb 2023
Copy link to clipboard 
Print this post

  Quote  Noise diode->amplifier->shaper->divider = random pulses for you to time.


Many years ago I was involved in putting together a quotation for a project to replace the UK premium bond drawing system (ERNIE) and this was the solution we proposed. Prior to that they used a system based on radioactive decay. I don't know what was eventually chosen and they are now on the 5th generation of ERNIE

Note to Nimue. Have you played with MATH(RAND) on the PicoMite? This uses the Mersenne Twister algorithm and is supposed to be much better than the C inbuilt function
Edited 2023-02-07 19:53 by matherp
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 10:38am 07 Feb 2023
Copy link to clipboard 
Print this post

I think I first saw a similar idea used in an old copy of Practical Wireless or somewhere like that. IIRC a neon was used rather than a noise diode - or maybe several of them. It's a long time ago now and I have brain fade...
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2642
Posted: 11:43am 07 Feb 2023
Copy link to clipboard 
Print this post

A good noise diode is the base-emitter junction of any transistor. Reverse bias it through a 1M resistor from 12V or more. The breakdown voltage is usually 7 to 9V.
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 02:51pm 07 Feb 2023
Copy link to clipboard 
Print this post

  matherp said  Note to Nimue. Have you played with MATH(RAND) on the PicoMite? This uses the Mersenne Twister algorithm and is supposed to be much better than the C inbuilt function


Yes - and that was the route into all this.   Asked how random is random and me digging a hole for myself.

If I can muster a few interested students I might get them to capture a couple of 1000 points and "do some stats".

Yet another rabbit hole I can ill afford ;-)

Cheers
N
Entropy is not what it used to be
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 03:08pm 07 Feb 2023
Copy link to clipboard 
Print this post

If you want a comparison, the Colour Maximite 2 has a true H/W random number generator which is used whenever you call RND
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 04:50pm 07 Feb 2023
Copy link to clipboard 
Print this post

  matherp said  If you want a comparison, the Colour Maximite 2 has a true H/W random number generator which is used whenever you call RND


Of course!!

Time to fire it up....


Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 04:50pm 07 Feb 2023
Copy link to clipboard 
Print this post

  Nimue said  
  matherp said  If you want a comparison, the Colour Maximite 2 has a true H/W random number generator which is used whenever you call RND


Of course!!

Time to fire it up....



How much geek in one photo ;-)

N
Entropy is not what it used to be
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 04:59pm 07 Feb 2023
Copy link to clipboard 
Print this post

That is true geekery in all its beauty. :)

I've not seen one of those Matsui boxes for ages.
Edited 2023-02-08 03:01 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 05:28pm 07 Feb 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  That is true geekery in all its beauty. :)

I've not seen one of those Matsui boxes for ages.


Not shown but the RF in is a great match for my Zeddy and Speccy.  B&W of course!

Another rabbit hole for the weeked.

N
Entropy is not what it used to be
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 05:35pm 07 Feb 2023
Copy link to clipboard 
Print this post

I really approve of the little Tardis too. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025