RNGCryptoServiceProvider 生成订单号
先生成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 生成订单号的更多相关文章
- PHP生成订单号(产品号+年的后2位+月+日+订单号)
require '../common.inc.php'; /* * 产品号+年的后2位+月+日+订单数 * @param [Int] $prodcutId 产品号 * @param [Int] $tr ...
- Oracle 函数 “自动生成订单号”
create or replace function get_request_code return varchar2 AS --函数的作用:自动生成订单号 v_mca_no mcode_apply_ ...
- php生成订单号-当天从1开始自增
/** * 生成订单号 * -当天从1开始自增 * -订单号模样:20190604000001 * @param Client $redis * @param $key * @param $back: ...
- php 生成订单号
最近在练手一个订单提交的小项目,需要用到生成订单号,网上找了下,觉得这个最好. function build_order_no(){ return date('Ymd').substr(implode ...
- php 生成订单号201807205598981
php版 /** * 生成唯一订单号 */ public function build_order_no() { $no = date('Ymd').substr(implode(NULL, arra ...
- 用存储过程生成订单号ID
DECLARE @sonumber BIGINTSELECT @sonumber=CONVERT(BIGINT, @serverId + Substring(CONVERT(VARCHAR(4), D ...
- json字符串转为php数组,使用随机字符串生成订单号(学生笔记)
//提交订单 function add_order(){ session_start(); // var_dump($_SESSION); // die(); // session_destroy() ...
- 13.如何生成订单号,用uuid
String orderNum = UUID.randomUUID().toString().replaceAll("-", "");
- 前台js根据当前时间生成订单号
*********前台显示框**************** <input type="text" id="WIDout_trade_no" name=& ...
随机推荐
- 浅谈C#委托的用法-delegate[转]
一.委托的概念 委托和类一样是一种用户自定义类型,它存储的就是一系列具有相同签名和返回类型的方法的地址,调用委托的时候,它所包含的所有方法都会被执行. 借用百度上的一句话概括:委托是一个类,它定义了方 ...
- 阿里云oss 直传
sts获取 参考https://help.aliyun.com/document_detail/28792.html?spm=a2c4g.11186623.6.786.6fb238dfI9iiqA 配 ...
- Win10安装4 —— 通过BIOS进入PE
本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12369367.html 一:"BIOS"与"PE& ...
- [Arc068D/At2299] Card Eater - 结论
[Arc068D/At2299] 有一堆牌,每张牌上有一个数字. 每次可以取出其中 \(3\) 张,丢掉数字最大的和数字最小的牌,把中间那张再放回牌堆. 要求最后所有剩余牌上的数字互不相同,求最多能剩 ...
- PHPstorm配置xdebug问题小记
安装的是符合自己环境的xdebug,因为是按照xdebug官网的步骤安装的:安装什么版本检测地址:https://xdebug.org/wizard.php,安装操作复制phpinfo()内容之后点击 ...
- 三行代码实现垂直居中和cube
三行代码实现上下居中 position: relative;top: 50%;transform: translateY(-50%); 效果如下: 代码: <!DOCTYPE html> ...
- linux - python:卸载
[root@test ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制删除已安装程序及其关联[root@test ~]# ...
- linux - mysql - 卸载:RPM包安装方式的MySQL卸载
(1)检查是否安装了MySQL组件 [root@DB-Server init.d]# rpm -qa | grep -i mysql MySQL-devel-5.6.23-1.linux_glibc2 ...
- python 音频可视化
代码整理好放在 github 上了: https://github.com/darkchii/visualize bilibili 演示视频:https://www.bilibili.com/vide ...
- mybatis-plus - MybatisPlusAutoConfiguration
mybatis 的通用maper, 其实有很多, mybatis-plus 算是其中做的比较好的一款了. 这里就来看一下 mybatis-plus 是怎么实现这个通用mapper功能的. 一. Bas ...