随机数算法

来源:互联网 发布:android sdk java版本 编辑:程序博客网 时间:2024/06/07 04:50
软件实现的算法都是伪随机算法,随机种子一般是系统时间
在数论中,线性同余方程是最基本的同余方程,“线性”表示方程的未知数次数是一次,即形如:
ax≡b (mod n)的方程。此方程有解当且仅当 b 能够被 a 与 n 的最大公约数整除(记作 gcd(a,n) | b)。这时,如果 x0 是方程的一个解,那么所有的解可以表示为:
{x0+kn/d|(k∈z)}
其中 d 是a 与 n 的最大公约数。在模 n 的完全剩余系 {0,1,…,n-1} 中,恰有 d 个解。
例子编辑
* 在方程
3x ≡ 2 (mod 6)
中, d = gcd(3,6) = 3 ,3 不整除 2,因此方程无解。
* 在方程
5x ≡ 2 (mod 6)
中, d = gcd(5,6) = 1,1 整除 2,因此方程在{0,1,2,3,4,5} 中恰有一个解: x=4。
* 在方程
4x ≡ 2 (mod 6)
中, d = gcd(4,6) = 2,2 整除 2,因此方程在{0,1,2,3,4,5} 中恰有两个解: x=2 and x=5。


纯线性同余随机数生成器
线性同余随机数生成器介绍:
古老的LCG(linear congruential generator)代表了最好最朴素的伪随机数产生器算法。主要原因是容易理解,容易实现,而且速度快。 


LCG 算法数学上基于公式:




X(0)=seed;
X(n+1) = (A * X(n) + C) % M;


其中,各系数为:
X(0)表示种子seed
模M, M > 0
系数A, 0 < A < M
增量C, 0 <= C < M
原始值(种子) 0 <= X(0) < M
其中参数c, m, a比较敏感,或者说直接影响了伪随机数产生的质量。


一般来说我们采用M=(2^31)-1 = 2147483647,这个是一个31位的质数,A=48271,这个A能使M得到一个完全周期


,这里C为奇数,同时如果数据选择不好的话,很有可能得到周期很短的随机数,例如


,如果我们去Seed=179424105的话,那么随机数的周期为1,也就失去了随机的意义。


(48271*179424105+1)mod(2的31次方-1)=179424105



[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package test;    
  2.     
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import java.util.concurrent.atomic.AtomicLong;  
  6.     
  7. public class Random {    
  8.         
  9.     public final AtomicLong seed=new AtomicLong();    
  10.     public final static long C = 1;    
  11.     public final static long A = 48271;    
  12.     public final static long M = (1L << 31) - 1;    
  13.         
  14.       
  15.     public Random(int seed){  
  16.         this.seed.set(seed);  
  17.     }  
  18.       
  19.     public Random(){  
  20.         this.seed.set(System.nanoTime());  
  21.     }  
  22.       
  23.     public long nextLong(){  
  24.         seed.set(System.nanoTime());  
  25.         return  (A *seed.longValue()  + C) % M;  
  26.     }  
  27.     public int nextInt(int number){  
  28.         return  new Long( (A * System.nanoTime() + C) % number).intValue();  
  29.     }  
  30.       
  31.         
  32.         
  33.     public static void main(String[] args) {    
  34.         System.out.println(new Random().nextLong());  
  35.         Map<Integer,Integer> map=new HashMap<Integer,Integer>();  
  36.         for(int i=0;i<100000;i++){  
  37.             int ran=new Random().nextInt(10);  
  38.             if(map.containsKey(ran)){  
  39.                 map.put(ran, map.get(ran)+1);  
  40.             }else{  
  41.                 map.put(ran, 1);  
  42.             }  
  43.               
  44.         }  
  45.         System.out.println(map);  
  46.     }    
  47. }    

自己写个简单例子,随机10万次,随机范围0到9,看看是否均匀


相对来说还是挺均匀的







队列


队列(常用数据结构之一)

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package data;  
  2.   
  3. /** 
  4.  * 隊列 
  5.  *  
  6.  * @author JYC506 
  7.  *  
  8.  */  
  9. public class Queue {  
  10.     /* 隊列大小 */  
  11.     private int maxSize;  
  12.     /* 存儲 */  
  13.     private long[] queryArray;  
  14.     /* 隊列頭 */  
  15.     private int head;  
  16.     /* 隊列尾 */  
  17.     private int end;  
  18.     /* 計數器 */  
  19.     private int counster;  
  20.   
  21.     /* 構造方法 */  
  22.     public Queue(int maxSize) {  
  23.         this.maxSize = maxSize;  
  24.         this.queryArray = new long[maxSize];  
  25.         this.head = 0;  
  26.         this.end = -1;  
  27.         this.counster = 0;  
  28.     }  
  29.   
  30.     /* 入隊 */  
  31.     public void add(long data) {  
  32.         if (this.end == this.maxSize - 1) {  
  33.             this.end = -1;  
  34.         }  
  35.         this.end++;  
  36.         this.queryArray[end] = data;  
  37.         this.counster++;  
  38.     }  
  39.   
  40.     /* 出對 */  
  41.     public long romve() {  
  42.         long data = this.queryArray[head];  
  43.         head++;  
  44.         if (this.head == maxSize) {  
  45.             this.head = 0;  
  46.         }  
  47.         this.counster--;  
  48.         return data;  
  49.     }  
  50.   
  51.     /* 是否為空 */  
  52.     public boolean isEmpty() {  
  53.         return this.counster == 0;  
  54.     }  
  55.     /*是否滿了*/  
  56.     public boolean isFull() {  
  57.         return this.counster == this.maxSize;  
  58.     }  
  59.   
  60.     public static void main(String[] args) {  
  61.         Queue que = new Queue(5);  
  62.         que.add(4);  
  63.         que.add(2);  
  64.         que.add(3);  
  65.         que.add(1);  
  66.         System.out.println(que.romve());  
  67.         System.out.println(que.romve());  
  68.         System.out.println(que.romve());  
  69.         System.out.println(que.romve());  
  70.     }  
  71. }  


栈(计算机术语)

栈 (stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个 栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除 掉,使其相邻的元素成为新的栈顶元素。

特点:先进先出

应用案例:1单词逆序
                 2分隔符匹配

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package data;  
  2. /** 
  3.  * 棧 
  4.  * @author JYC506 
  5.  * 
  6.  */  
  7. public class Stack {  
  8.     /* 栈的大小 */  
  9.     private int maxSize;  
  10.     /* 用来存放 */  
  11.     private long[] stackArray;  
  12.     /* 栈顶 */  
  13.     private int top;  
  14.   
  15.     public Stack(int maxSize) {  
  16.         this.maxSize = maxSize;  
  17.         this.stackArray = new long[maxSize];  
  18.         this.top=-1;  
  19.     }  
  20.   
  21.     /* 入栈 */  
  22.     public void push(long data) {  
  23.         top++;  
  24.         stackArray[top]=data;  
  25.   
  26.     }  
  27.   
  28.     /* 出栈 */  
  29.     public long pop() {  
  30.        long data=stackArray[top];  
  31.        top=top-1;  
  32.        return data;  
  33.     }  
  34.   
  35.     /* 查看 */  
  36.     public long peek() {  
  37.     return stackArray[top];  
  38.     }  
  39.     /*是否为空*/  
  40.     public boolean isEmpty(){  
  41.         return top==-1;  
  42.     }  
  43.   
  44.       
  45.     public boolean isFull(){  
  46.         return maxSize-1==top;  
  47.     }  
  48.       
  49.     public static void main(String[] args) {  
  50.         Stack stack=new Stack(5);  
  51.         stack.push(1);  
  52.         stack.push(2);  
  53.         stack.push(3);  
  54.         System.out.println(stack.pop());  
  55.         System.out.println(stack.pop());  
  56.         System.out.println(stack.pop());  
  57.     }  
  58. }  



0 0
原创粉丝点击