`
jimmee
  • 浏览: 557975 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

(转载)Pseudo-Random Number Generation Routine for the MAX765x Microprocessor

    博客分类:
  • J2SE
阅读更多
Abstract: This application note gives a function for random number generation using the MAX7651/52 microcontroller with 12-bit analog-to-digital converter (ADC).

Applications such as spread-spectrum communications, security, encryption and modems require the generation of random numbers. The most common way to implement a random number generator is a Linear Feedback Shift Register (LFSR). Codes generated by a LFSR are actually "pseudo" random, because after some time the numbers repeat. The trick is to use a shift register of sufficient length so that the pattern repeats after some extremely long time.

A basic LFSR of length five is shown in Figure 1. The shift register is a series-connected group of flip-flops, with XOR feedback. An XOR gate is use to scramble the input bit.

Figure 1. 5-stage linear feedback shift register.
Figure 1. 5-stage linear feedback shift register.

There are tables that give the proper feedback tap position for generating sequences that take the maximum number of clocks to repeat. Such a table is shown below:

Table 1. Taps for Maximal-Length LFSRs with 2 to 32 Bits
No. of bits Length of Loop Taps
2 3* [0,1]
3 7* [0,2]
4 15 [0,3]
5 31* [1,4]
6 63 [0,5]
7 127* [0,6]
8 255 [1,2,3,7]
9 511 [3,8]
10 1023 [2,9]
11 2047 [1,10]
12 4095 [0,3,5,11]
13 8191* [0,2,3,12]
14 16,383 [0,2,4,13]
15 32,767 [0,14]
16 65,535 [1,2,4,15]
17 131,071* [2,16]
18 262,143 [6,17]
19 524,287* [0,1,4,18]
20 1,048,575 [2,19]
21 2,097,151 [1,20]
22 4,194,303 [0,21]
23 8,388,607 [4,22]
24 16,777,215 [0,2,3,23]
25 33,554,431 [7,24]
26 67,108,863 [0,1,5,25]
27 134,217,727 [0,1,4,26]
28 268,435,455 [2,27]
29 536,870,911 [1,28]
30 1,073,741,823 [0,3,5,29]
31 2,147,483,647* [2,30]
32 4,294,967,295 [1,5,6,31]


* Sequences whose length is a prime number

Be aware that there are multiple solutions for tap positions to generate maximum length sequences.

There is one major issue with using LFSRs: if all stages happen to be '0', the shift register gets "stuck". This is because the XOR of all '0's is still '0'. The XOR feedback does not produce a '1' to get the sequence started again. To prevent this condition, the routine must be first loaded with a non-zero seed value. This value can be any number at all, as long as it is not zero. The numbers generated by the LFSR are based on the seed value. You will get the same sequence of numbers, over and over, unless at some point the LSFR is reloaded with a different seed.

Where does this seed value come from? It will depend on what is available in your particular application. For example, if your system has access to a RTC (real-time clock), then a good seed is based off the time. You can read the current time and/or date, mask off portions and use that as a seed. Another example is temperature. If your system can read temperature (assuming it's not constant) then that can make a good seed. The ADC of the MAX765x can be set to read all sorts of things: scales AC power line voltage, some sensor position or even amplified Johnson noise from a Zener diode (a common practice in cryptography).

However, in some cases you will just have to use 01H or some other number, and accept the fact that the sequence will repeat eventually and in a predetermined pattern.

The routine presented uses a 25-bit sequence, which repeats after being called 33 million times. Even if you cannot produce a unique seed each time, the length is such that in most applications, the "randomness" is more than sufficient.

The MAX765x listing is shown below. The routine uses four 8-bit memory locations labeled RN1-RN4. The lower 3 bytes RN1-RN3 are used for 24 bits, and the MSB of RN4 is the 25th bit. The algorithm uses XOR feedback (using the processor's XRL instruction) from "stages" 25 (the Carry bit) and stage 7 (the MSB of RN1). Since all of the resisters are simply RAM locations, you can form up to 32-bit wide random numbers. For this example, an 8-bit number RANNUM is stored in RAM at the end of the routine.

To get a true Gaussian distribution function of the random numbers, you can do further processing. Adding some arbitrary number of consecutive samples and taking the average (say 4) will create a Gaussian distribution.

One programming 'trick' which is used in the algorithm are "byte swaps" to simulate a "shifting by 8 clocks". This is to save CPU clock cycles. For example, if the original byte order was ABCD, after the byte-swaps the order is BCDA. This prevents the code from having to do "housekeeping" from shifting a byte's MSB into the next byte's LSB. It doesn't matter if the random numbers are calculated every clock or every 8 clocks: they are still random. Since the total length of the LFSR is a product of 3 prime numbers (31, 601 and 1801) the sequence is still 33,554,431 subroutine calls until the sequence repeats! Of course, since we are looking at 8 of the bits for our example, the values are restricted to 00H to 0FFH, and so the same value will be returned multiple times.

Another cycle-saving 'trick' used is when the XOR of the 2 bits of interest is executed, the entire contents of RN1 is altered (see the comment below).

;
; SUBROUTINE RANDOM
;
; GENERATES RANDOM NUMBERS USING 25 BIT SHIFT REGISTER
; WITH XOR FEEDBACK ON STAGES 7 AND 25 (MAX CYCLE LENGTH)
;
; IN ORDER FOR THIS ROUTINE TO WORK, THERE ARE 2 THINGS TO CONSIDER:
; A) SOME TYPE OF NON-ZERO VALUE ‘SEED’ MUST FIRST BE LOADED INTO RN1
: B) THE SEQUENCE WILL REPEAT AFTER APPROX 33 MILLION CALLS TO THE ROUTINE
;
; CPU RESOURCES REQUIRED:
;
; 6 RAM LOCATIONS AS FOLLOWS
;
; RANNUM: THE CALCULATED RANDOM NUMBER
; RN1-RN1: FORMS THE SHIFT REGISTER
; TEMP1: A TEMPORARY SCRATCH-PAD REGISTER
;
; THE ROUTINE DESTROYS THE A REGISTER AND THE CARRY BIT
;
;
RANDOM: MOV     A,RN4           ; BIT 25 TO CARRY
        RRC     A
        MOV     RN4,RN3
        MOV     RN3,RN2
        MOV     RN2,RN1         ; SHIFT ALL BYTES LEFT
        MOV     A,RN4           ; WAS RN3
        RRC     A               ; GET BIT 25 (IN CARRY) INTO OLD RN3
        MOV     TEMP1,A         ; SAVE IT FOR LATER
        MOV     A,RN1
        RLC     A               ; BIT 1 TO 7 IN POSITION
        MOV     RN1,A           ; PUT BACK
        MOV     A,TEMP1         ; RESTORE A
        XRL     A,RN1           ; MOD 2 SUM OF ALL BITS IN RN1 AND THE CARRY BIT
        MOV     TEMP1,A         ; SAVE IT
        MOV     A,RN4
        RRC     A
        MOV     RN4,A           ; ROTATE RN4
        MOV     A,RN3
        RRC     A
        MOV     RN3,A           ; ROTATE RN3
        MOV     A,RN2
        RRC     A
        MOV     RN2,A           ; ROTATE RN2
        MOV     A,TEMP1         ; RESTORE A
        RLC     A
        MOV     RN1,A ; xor OVER-WRITES ALL 8 BITS, BUT DOESN’T MATTER
MOV     RANNUM,A        ; RANDOM NUMBER!!
        RET

Related Parts   APP 1743: Sep 25, 2002
MAX7651 Flash Programmable 12-Bit Integrated Data-Acquisition Systems Full Data Sheet
(PDF, 488kB)
MAX7652 Flash Programmable 12-Bit Integrated Data-Acquisition Systems Full Data Sheet
(PDF, 488kB)
Automatic Updates
Would you like to be automatically notified when new application notes are published in your areas of interest? Sign up for EE-Mail™.

Your Comments
Login or register to post a comment.

分享到:
评论

相关推荐

    2016-Pseudo-Random High-Frequency Square-Wave Voltage Injection

    王教授的论文“2016-Pseudo-Random High-Frequency Square-Wave Voltage Injection”探讨了一种基于伪随机高频方波电压注入的无传感器控制技术,用于降低内转子永磁同步电机(IPMSM)驱动系统的可听噪声。...

    Color_pseudo-random_array_in_the_application_of_th_三维 结构光_伪随机_结构

    是采用伪随机编码结构光照明主动视觉技术,用编码结构光照明被测场景,实现动态三维场景的重建

    Creat_pseudo-random Numbers_random_逆变法_伪随机数_stickdrq_python_

    标题中的“Creat_pseudo-random Numbers_random_逆变法_伪随机数_stickdrq_python_”表明我们关注的是一个Python程序,它使用了逆变法来生成一系列的伪随机数。逆变法是一种通过数学变换将已知的随机序列转换为新...

    前端开源库-pseudo-elements

    标题“前端开源库-pseudo-elements”指向的是一个专门收集和整理CSS伪元素相关资源的开源项目,可能包含了一个详尽的伪元素列表,以及如何在实际项目中使用它们的示例和最佳实践。 描述中的“所有CSS伪元素的列表”...

    generation--of-random-data.rar_random_随机数生成

    随机数生成器(Random Number Generator, RNG)通常可以分为伪随机数生成器(Pseudo-Random Number Generator, PRNG)和真随机数生成器(True Random Number Generator, TRNG)。PRNG基于数学算法,通过一个初始种子...

    Robust Pseudo Random Fields for Light-Field Stereo Matching

    Based on pseudo-likelihood, it applies soft expectation-maximization (EM) for good model fitting and hard EM for robust depth estimation. We introduce novel pixel difference models to enable such ...

    # End-to-end Pseudo-LiDAR for Image-Based 3D Object Detection Th

    本篇文章将深入探讨"End-to-end Pseudo-LiDAR for Image-Based 3D Object Detection"这一主题,以及如何通过伪LiDAR技术实现视觉与LiDAR的高效融合。 首先,我们要理解什么是伪LiDAR。传统的LiDAR系统能够提供高...

    Python库 | pseudo-python-0.2.12.tar.gz

    在Python生态系统中,有许多开源库可供选择,其中之一就是"pseudo-python"。这个库的版本为0.2.12,它被打包成一个名为"pseudo-python-0.2.12.tar.gz"的压缩文件。该文件格式通常是Linux和Unix系统中常见的tarball,...

    前端开源库-has-pseudo-class

    "has-pseudo-class"就是一个专注于处理CSS伪类的前端开源库。这个库的核心功能是检测一个CSS选择器是否包含有伪类,这对于理解和优化CSS选择器的使用,以及在动态渲染时避免不必要的计算,有着重要的意义。 伪类是...

    Random Number Generation and Monte Carlo Methods

    伪随机数生成器(Pseudo-Random Number Generator, PRNG)是一类算法,用于生成一系列数字,这些数字在统计上具有随机性特征,但实际上是由确定性的算法产生的。一个好的PRNG应该满足以下条件: 1. **均匀性**:生成...

    Golomb S.W. Gong G.(2005) Signal Design for Good Correlation(438s).pdf

    This book provides a comprehensive, up-to-date description of the methodologies and the application ... and pseudo-random sequence generation for secure authentication and for stream cipher cryptology.

    PSEUDO_RANDOM_ise9migration.zip_pseudo random_random

    在IT领域,伪随机数生成器(Pseudo-Random Number Generator, PRNG)是一个至关重要的工具,广泛应用于各种计算任务,如加密、模拟、游戏、图形渲染等。标题中的"PSEUDO_RANDOM_ise9migration.zip_pseudo random_...

    cod_GPS.zip_The Number

    The function is used to generate a pseudo-random code generation of any GPS satellite 37 C / A codes Sv_id - satellite number cod - a vector comprising an output sequence g2s - vector containing the ...

    Distributed fiber temperature sensor using a laser diode modulated by a pseudo-random bit sequence

    We propose and demonstrate a scheme to measure the distribution of temperature along an optical fiber based on pseudo-random sequence modulation. In our experimental work, current modulation with a ...

    前端开源库-pseudo-elements.zip

    这个压缩包“前端开源库-pseudo-elements.zip”可能包含了一些关于`pseudo-elements`的示例、教程或工具,旨在帮助开发者更深入地理解和运用这个技术。 `pseudo-elements`主要有以下几种: 1. `::before`: 这个伪...

    pseudo-random-number-generators:伪随机数生成器在 C 语言中的实现和使用 TestU01 的评估 :game_die:

    伪随机数生成器(Pseudo-Random Number Generators, PRNGs)是计算机科学中一个重要的概念,特别是在模拟、加密、游戏开发以及各种统计计算中。C 语言因其高效和跨平台特性,常被用于实现这类算法。本主题将深入探讨...

    Cryptograhpy Theory and Practice 第三版

    The third part contains chapters on selected research-oriented topics, namely, authentication codes, secret sharing schemes, pseudo-random number generation, and zero-knowledge proofs。。。

Global site tag (gtag.js) - Google Analytics