随机订单号

本文介绍了一种使用C#生成唯一订单号的方法,通过结合当前时间和随机数确保其唯一性。同时,展示了如何创建小于指定数值的随机数,适用于多种随机需求场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 生成订单
{
    class Program
    {
        static void Main(string[] args)
        {

            //string.Format("{0:yyyyMMddHHmmssfff}{1}", DateTime.Now, 200.Next())
            Console.WriteLine(200.Next());
            //string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), 
            Console.WriteLine(GetUniqueKey());

        }

        private static string GetUniqueKey()
        {
            //int maxSize = 8;
            //int minSize = 5;
            //char[] chars = new char[62];
            //string a;
            //a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            //chars = a.ToCharArray();
            //int size = maxSize;
            //byte[] data = new byte[1];
            //RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            //crypto.GetNonZeroBytes(data);
            //size = maxSize;
            //data = new byte[size];
            //crypto.GetNonZeroBytes(data);
            //StringBuilder result = new StringBuilder(size);
            //foreach (byte b in data)
            //{
            //    result.Append(chars[b % (chars.Length - 1)]);
            //}
            //return result.ToString();

            byte[] randomBytes = new byte[8];
            System.Security.Cryptography.RNGCryptoServiceProvider rngServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rngServiceProvider.GetBytes(randomBytes);
            int result = BitConverter.ToInt32(randomBytes, 0);
            result = System.Math.Abs(result);  //求绝对值
            return result.ToString();
        }


    }

    public static class Test
    {
    /// <summary>   
    /// 生成小于输入值绝对值的随机数   
    /// </summary>   
    /// <param name="NumSides"></param>   
    /// <returns></returns>   
    public static int Next(this int numSeeds)
    {
        numSeeds = Math.Abs(numSeeds);
        if (numSeeds <= 1)
        {
            return 0;
        }

        int length = 4;
        if (numSeeds <= byte.MaxValue)
        {
            length = 1;
        }
        else if (numSeeds <= short.MaxValue)
        {
            length = 2;
        }

        return Next(numSeeds, length);
    }

    private static int Next(int numSeeds, int length)
    {
        // Create a byte array to hold the random value.   
        byte[] buffer = new byte[length];
        // Create a new instance of the RNGCryptoServiceProvider.   
        System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
        // Fill the array with a random value.   
        Gen.GetBytes(buffer);
        // Convert the byte to an uint value to make the modulus operation easier.   
        uint randomResult = 0x0;//这里用uint作为生成的随机数   
        for (int i = 0; i < length; i++)
        {
            randomResult |= ((uint)buffer[i] << ((length - 1 - i) * 8));
        }
        // Return the random number mod the number   
        // of sides.  The possible values are zero-based   
        return (int)(randomResult % numSeeds);
    }
}
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值