rand ?

Petr Krc pekr
Středa Březen 17 11:54:20 CET 2004


Hi,

Nekolik prikladu realizace generatoru nahodnych cisel.
V tom prvnim zapojeni je pouzit prechod BE Germanioveho tranzistoru
v zavernem smeru. Teoreticky by sel nahradit zenerovou diodou.

-- 
Regards
           Petr Krc

------------------------------------------------------------------

as more people wished, here is the true h/w random number generator:

 White noise generator, design by dr. Imre Bartfai, 8/30/1999

 +--------------------------------------------------------------------- Vdd
 |                           220k                    220k               5V
 |                       +---\/\/\---+           +---\/\/\---+------+
 |                       |           |           |           |      |
 | Ge                    |    |\     |           |    +------+      \
 | EFT317 PNP      +-----+----|- \   |           |    |             / 220k
 |      C          |     |    |    >-+----| |----+    |     |\      \
 |   |/            \  +-------|+ /        47n    |    +-----|- \    |
 +--B|        220k /  |  |    |/                 |    |     |    >--+
     |\            \  |  |  1/2 LM358            +----------|+ /    |
       |E          |  |  |                       |    |     |/      \
       +----| |----+--+  +                       +    +             /  47k
       |    47n    |     |                       |    | 1/2 LM358   \
       \           \     \                       \    \             |
  4k7  /        1M /     / 2k2                1M /    / 2k2         + out
       \           \     \                       \    \
       |           |     |                       |    |
       +-----------+-----+-----------------------+----+---------------- Vss

 Standard TTL can not be driven, but CMOS (and maybe LS) are o.k.
 The power supply must be filtered thoroughly due to the high gain!

------------------------------------------------------------------

Nikolai Golovchenko <golovchenko@MAIL.RU> wrote:

I think that you need an n-bit random number generator that would satisfy
the number of steps in the interval. Here is an 8-bit example (sorry, no
code). I came across it somewhere in TI's MSP430 application notes some time
ago.

rnd(n) = rnd(n-1)*mult+inc

n - iteration number
rnd(0)= 123 - seed number
mult = 221 - multiplier
inc = 54 - prime number


(I modeled this in MATLAB. Looks ok)

Note, that result may be greater than 8 bit. Just leave only lower 8 bits.

This will generate pseudo-random sequence.

------------------------------------------------------------------

;   Pseudorandom digital white noise generator for PIC series.
;
;   PIC12C508 version written April 7th, 1997 by Scott Rider.
; Questions/comment to chip@aeug.org
;
;   PIC12C509 modified version by Antonio L. Benci. 22/03/1999.
;
;   This program uses Microchip assembler mnemonics and the Microchip
; MPASM assembler (http://www.microchip.com/).  Default config options
; are set in the __FUSES line below: MCLRE off, CP off, WDT off,
OSC=INTRC.
;
;                  _______  _______
;                 |       \/       |
;     (+5V) Vdd --+ 1            8 +-- Vss (GND)
;                 |                |                      470
;   X1/CLKI/GP5 --+ 2            7 +-- GP0 (audio out) --/\/\/-- (spkr)
;    (not used)   |                |                  |   10K   1uF
;   X2/CLKO/GP4 --+ 3            6 +-- GP1             --/\/\/--|(--
(line)
;    (swt input)  |                |  (swt input)
;     GP3/!MCLR --+ 4            5 +-- GP2
;    (swt input)  |  (PIC12C508)   |  (swt input)
;                 +----------------+
;
;   This program generates a continuous stream of white noise through a
; speaker connected (via a 470-ohm series resistor) to pin GP0 of a
; PIC12C509.  It demonstrates an easy method for generating a seemingly
; random stream of bits. Hardware folks will probably recognize it as
the
; tapped shift register technique covered in the CMOS Cookbook by Don
; Lancaster.
;
; Adapted from the original PIC16C54 (Parallax) version by Scott
Edwards.
;
;##########################################################################
;
; Programmable noise weighting added by A L Benci 22/03/1999
;
; GP1..GP4 are connected to a dip switch which the user can use to
select
; 'textures' of 2 to 30. A zero value skips the function.
;
;
        list    p=12c509
        radix   dec
        include "p12c509.inc"

        __FUSES _MCLRE_OFF &_CP_OFF & _WDT_OFF & _IntRC_OSC  ;Internal osc.

        cblock  0x07    ;Variables
                i       ;Loop counter
                x       ;Scratch
                y       ;Scratch
                zz      ;Scratch
                sr0     ;32 bits of shift register
                sr1
                sr2
                sr3     ;/
        endc

        org      0x0
;
;  PIC12C509 cold starts at 03FFF (where it loads a factory-programmed
; internal osc. calibrate value).  It then rolls over to 0x0.
;
        movwf   OSCCAL
        goto    start
;
;  Subroutines in 12-bit PIC series must go on bottom page of ROM.
;
wait
loop    decfsz  i,F
        goto    loop
        retlw   0
;
;  Main code area traditionally starts at 0x100 on a 12-bit PIC.
;
        org     0x100

start   movlw   b'11000000'     ;Turn off T0CKI
        option                  ;/
        movlw   b'00000000'     ;Preset outputs to come up low
        movwf   GPIO            ;/
        movlw   b'00011110'     ;Set the I/O direction (GP3..GP1 as inputs,
GP0,GP5 as output)
        tris    GPIO

        clrf    sr0             ;Might as well start with a seed of zero.
        clrf    sr1
        clrf    sr2
        clrf    sr3             ;/
;
;   The main loop 'taps' is 24 CPU cycles.  At the 4MHz internal clock
; rate, the noise output is operating at a max. rate of about 21KHz.
;
taps    btfss   sr3,6           ;Get bits 18 and 31 for the feedback
XOR.
        bcf     x,0
        btfsc   sr3,6
        bsf     x,0             ;Bit 31 -> x

        btfss   sr2,1
        bcf     y,0
        btfsc   sr2,1
        bsf     y,0             ;Bit 18 -> y

        movf    y,W             ;!(x XOR y) -> carry
        xorwf   x,F

        btfsc   x,0
        bcf     STATUS,C
        btfss   x,0
        bsf     STATUS,C        ;/

        rlf      sr0,F          ;32-bit shift
        rlf      sr1,F
        rlf      sr2,F
        rlf      sr3,F          ;/
;
;  Put a pseudorandom bit out to the speaker.  In this example, GPIO bit
0
; (pin 7 on the PIC12C509) is used.  Be sure to use a 470 ohm resistor
for
; speaker or 10K or better for input to an op-amp/divider/whatever.
;

        btfss   sr3,7           ;line 1
        bcf     GPIO,0          ;line 2
        btfsc   sr3,7           ;line 3
        bsf     GPIO,0          ;line 4

;
;  Uncomment the following 'call wait' to experiment with changing the
; 'texture' of the noise (see 'wait' routine, above).

        movf     GPIO,W         ;read switch input
        andlw    b'00011110'    ;mask all but swt bits
        movwf    i              ;save value

        btfss    STATUS,Z       ;is result zero
        call     wait           ;Optional delay routine to alter sound

        goto     taps           ;Ad infinitum...
;
;  That's all, folks!
;
        end

--
Antonio (Nino) Benci

------------------------------------------------------------------







Další informace o konferenci Hw-list