先生成1~1000的随机数

class Program
{
// Create a new instance of the RNGCryptoServiceProvider.
private static System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
int a = NextRandom(, , rng);
Console.WriteLine(string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), a.ToString().PadLeft(, '')));
}
Console.Read();
}
private static int NextRandom(int numSeeds, int length, System.Security.Cryptography.RNGCryptoServiceProvider rng)
{
// Create a byte array to hold the random value.
byte[] randomNumber = new byte[length];
// Fill the array with a random value.
rng.GetBytes(randomNumber);
// Convert the byte to an uint value to make the modulus operation easier.
uint randomResult = 0x0;
for (int i = ; i < length; i++)
{
randomResult |= ((uint)randomNumber[i] << ((length - - i) * ));
}
return (int)(randomResult % numSeeds) + ;
}
}

注意将RNGCryptoServiceProvider定义为循环外的静态变量。

经测试,这样在并发不大的时候能保证订单号的唯一性。

RNGCryptoServiceProvider 生成订单号的更多相关文章

  1. PHP生成订单号(产品号+年的后2位+月+日+订单号)

    require '../common.inc.php'; /* * 产品号+年的后2位+月+日+订单数 * @param [Int] $prodcutId 产品号 * @param [Int] $tr ...

  2. Oracle 函数 “自动生成订单号”

    create or replace function get_request_code return varchar2 AS --函数的作用:自动生成订单号 v_mca_no mcode_apply_ ...

  3. php生成订单号-当天从1开始自增

    /** * 生成订单号 * -当天从1开始自增 * -订单号模样:20190604000001 * @param Client $redis * @param $key * @param $back: ...

  4. php 生成订单号

    最近在练手一个订单提交的小项目,需要用到生成订单号,网上找了下,觉得这个最好. function build_order_no(){ return date('Ymd').substr(implode ...

  5. php 生成订单号201807205598981

    php版 /** * 生成唯一订单号 */ public function build_order_no() { $no = date('Ymd').substr(implode(NULL, arra ...

  6. 用存储过程生成订单号ID

    DECLARE @sonumber BIGINTSELECT @sonumber=CONVERT(BIGINT, @serverId + Substring(CONVERT(VARCHAR(4), D ...

  7. json字符串转为php数组,使用随机字符串生成订单号(学生笔记)

    //提交订单 function add_order(){ session_start(); // var_dump($_SESSION); // die(); // session_destroy() ...

  8. 13.如何生成订单号,用uuid

    String orderNum = UUID.randomUUID().toString().replaceAll("-", "");

  9. 前台js根据当前时间生成订单号

    *********前台显示框**************** <input type="text" id="WIDout_trade_no" name=& ...

随机推荐

  1. java - 各类OOM分析

    StackOverflowError 比较常见的问题,虚拟机栈中栈帧过多超出栈容量,常见发生在递归方法深度过深. OutOfMemoryError java heap space java堆内存不足以 ...

  2. ASP.NET MVC5 的请求管道和运行生命周期

    https://www.jianshu.com/p/848fda7f79e0 请求处理管道 请求管道是一些用于处理HTTP请求的模块组合,在ASP.NET中,请求管道有两个核心组件:IHttpModu ...

  3. maven-设置aliyun远程库

    maven默认的远程库下载起来非常慢,习惯改成aliyun的库. 一.修改maven配置 打开maven配置文件setting.xml,改mirror <mirrors> <mirr ...

  4. Vuejs开发环境的搭建

    Windows系统上搭建VueJS开发环境 1.安装node.js:在node.js官网下载对应系统的msi包并安装 注:node的安装分全局和本地模式.一般情况下会以本地模式运行,包会被安装到和你的 ...

  5. H3C端口隔离

    一.端口隔离简介 为了实现端口间的二层隔离,可以将不同的端口加入不同的VLAN,但VLAN资源有限.采用端口隔离特性,用户只需要将端口加入到隔离组中,就可以实现隔离组内端口之间二层隔离,而不关心这些端 ...

  6. PP: Shallow RNNs: a method for accurate time-series classification on tiny devices

    Problem: time series classification shallow RNNs: the first layer splits the input sequence and runs ...

  7. SpringMVC组件解析

    SpringMVC组件解析 1. 前端控制器:DispatcherServlet 用户请求到达前端控制器,它就相当于 MVC 模式中的 C,DispatcherServlet 是整个流程控制的中心,由 ...

  8. Linux - Shell - 算数表达式 - 位运算

    概述 shell 中基于 $(()) 的 位运算 背景 复习 shell 脚本 凑数吧 准备 环境 os centos7 1. 位运算 代码 #!/bin/bash # 位运算 arg1=2 arg2 ...

  9. COMMUNITY DETECTION_python-louvain

    Python-louvain Package pip install python-louvain import community #first compute the best partition ...

  10. AD转化器分类及特点和选用

    1. AD转换器的分类 下面简要介绍常用的几种类型的基本原理及特点:积分型.逐次逼近型.并行比较型/串并行型.∑-Δ调制型.电容阵列逐次比较型及压频变换型. 1)积分型(如TLC7135)积分型AD工 ...