c# 常用操作保留
RanDom如何提高生成随机数的随机性
一个在线考试系统的项目,需要从题库中随机抽取试题,但是如果直接 Random ran=new Randon(),ran.Next(nummin,nummax);的方法,
在系统执行过快的情况下,造成只取到了一系列连续的题目,其他的题目均没有取到,用下面提高随机数种子的方法搞定
用C#的Random函数连续的生成随机数,会发现结果往往都是一个值。原因就是Random函数需要一个种子,人为指定的方式是Random(i),如果没有明确指定种子的话,系统会自动用当前系统时间作为种子。如果程序执行过快就会造成,种子一样的情况,这时候产生的随机数就是同一个值。解决方案如下:
Random类是一个产生伪随机数字的类,它的构造函数有两种,一个是直接New Random(),另外一个是New Random(Int32),前者是根据触发那刻的系统时间做为种子,来产生一个随机数字,后者可以自己设定触发的种子,一般都是用UnCheck((Int)DateTime.Now.Ticks)做为参数种子,因此如果计算机运行速度很快,如果触发Randm函数间隔时间很短,就有可能造成产生一样的随机数,因为伪随机的数字,在Random的内部产生机制中还是有一定规律的,并非是真正意义上的完全随机。
Random快速连续产生相同随机数的解决方案:
1、延时的办法。
可以采用for循环的办法,也可以采用Thread.Sleep(100);
2、提高随机数不重复概率的种子生成方法:
static int GetRandomSeed( )
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider( );
rng.GetBytes( bytes );
return BitConverter.ToInt32( bytes , 0 );
}
Random random = new Random( GetRandomSeed( ) ); 引用:http://blog.sina.com.cn/s/blog_67f16f3d0100xsdd.html
RanDom提高种子随机率
c#不同进制之间的相互转换
//十进制转二进制
Console.WriteLine(Convert.ToString(69, 2));
//十进制转八进制
Console.WriteLine(Convert.ToString(69, 8));
//十进制转十六进制
Console.WriteLine(Convert.ToString(69, 16));
//二进制转十进制
Console.WriteLine(Convert.ToInt32(”100111101″, 2));
//八进制转十进制
Console.WriteLine(Convert.ToInt32(”76″, 8));
//C# 16进制转换10进制
Console.WriteLine(Convert.ToInt32(”FF”, 16)); 进制转换
MD5 加密
/// <summary>
///32位 md5加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetMD5_32(string str)
{
byte[] arrHashInput;
byte[] arrHashOutput;
//MD5CryptoServiceProvider objMD5 = new MD5CryptoServiceProvider();
MD5 objMD5 = new MD5CryptoServiceProvider();
arrHashInput = C2B(str);
arrHashOutput = objMD5.ComputeHash(arrHashInput);
return BitConverter.ToString(arrHashOutput);
}
protected static byte[] C2B(string str)
{
char[] arrChar;
arrChar = str.ToCharArray();
byte[] arrByte = new byte[arrChar.Length];
for (int i = 0; i < arrChar.Length; i++)
{
arrByte[i] = Convert.ToByte(arrChar[i]);
}
return arrByte;
}
32位 MD5加密
/// <summary>
/// 16位 MD5加密
/// </summary>
/// <param name="sInputString"></param>
/// <returns></returns>
public static string GetMD5_16(string sInputString)
{
byte[] data = Encoding.UTF8.GetBytes(sInputString);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
string sKey = GenerateKey();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return BitConverter.ToString(result);
} // 创建Key
public static string GenerateKey()
{
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
// 加密字符串
public static string EncryptString(string sInputString, string sKey)
{
byte[] data = Encoding.UTF8.GetBytes(sInputString);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return BitConverter.ToString(result);
}
// 解密字符串
public static string DecryptString(string sInputString, string sKey)
{
string[] sInput = sInputString.Split("-".ToCharArray());
byte[] data = new byte[sInput.Length];
for (int i = 0; i < sInput.Length; i++)
{
data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
}
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateDecryptor();
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return Encoding.UTF8.GetString(result);
}
16位 MD5加密
防止F5窗口刷新
public bool IsRefurbish { get { return this.Request.Headers["Accept"] == "*/*"; } }
c# 常用操作保留的更多相关文章
- java 常用操作(保留小数位数、int转string,string转int)
1.保留2位小数 //similarityTemp为double类型,需要保留2位有效数据,利用String.format String strTemp=String.format("%.2 ...
- Python 基礎 - 字符串常用操作
字符串常用操作 今天就介紹一下常用的字符串操作,都是以 Python3撰寫的 首字母變大寫 #!/usr/bin/env python3 # -*- coding:utf-8 -*- name = & ...
- linux下关于gz和bz2压缩格式的常用操作技巧
.gz和.bz2都是linux下压缩文件的格式,有点类似windows下的.zip和.rar文件..bz2和.gz的区别在于,前者比后者压缩率更高,后者比前者花费更少的时间. 也就是说同一个文件,压缩 ...
- Adb工具常用操作-转(二)
一. PC与模拟器或真机交换文件(adb pull和adb push) 在开发阶段或其他原因,经常需要将PC上的文件复制到模拟器或真机上,或将模拟机和真机上的文件复制到PC上.使用adb pull和a ...
- Adb工具常用操作(一)
一.启动或关闭server 1.3 Android SDK中的常用命令行工具 在<Android SDK安装目录>\tools目录中带了很多命令行工具.虽然一般的开发人员并不需要完全掌握 ...
- Linux常用操作练习
Linux常用操作练习 练习一:安装CentOS 1.设置为1G内存(才有图形界面).10G硬盘 2.分给交换分区2G(4G一下2G,8G-32G分4G-8G) 练习二:安装CentOS迷你版 1.安 ...
- js,jQuery数组常用操作小结
一.js中数组常用操作小结 (1) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift() ...
- vim常用操作技巧与配置
vi是linux与unix下的常用文本编辑器,其运行稳定,使用方便,本文将分两部分对其常用操作技巧和配置进行阐述,其中参考了网上的一些文章,对作者表示感谢 PART1 操作技巧 说明: 以下的例子中 ...
- 容器常用操作 - 每天5分钟玩转 Docker 容器技术(25)
前面讨论了如何运行容器,本节学习容器的其他常用操作. stop/start/restart 容器 通过 docker stop 可以停止运行的容器. 容器在 docker host 中实际上是一个进程 ...
随机推荐
- android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[问题已解决]
11-06 18:29:15.582: W/WebView(27807): java.lang.Throwable: A WebView method was called on thread 'Ja ...
- ylbtech-LanguageSamples-NamedAndOptional(命名和可选参数)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-NamedAndOptional(命名和可选参数) 1.A,示例(Sample) 返回顶 ...
- (转)Akka学习笔记
Akka学习笔记系列文章: <Akka学习笔记:ACTORS介绍> <Akka学习笔记:Actor消息传递(1)> <Akka学习笔记:Actor消息传递(2)> ...
- android 带文字阴影的button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- 图解aclocal、autoconf、automake、autoheader、configure
http://www.laruence.com/2008/11/11/606.html 本文地址: http://www.laruence.com/2008/11/11/606.html 转载文章 原 ...
- 关于js加密解密
有的时候有些网站的js用简单的eval混淆加密了.解密其实很简单的 解密JS的eval加密码的方式例如这段: 很多朋友以为这段代码是“加密”的,其实这也谈不上是加密,只能算是一种编码(Encode)或 ...
- 安装ADT的时候,提示“Cannot complete the install because one or more required items could not be
今天在安装ADT的时候,提示: Cannot complete the install because one or more required items could not be found. S ...
- SOA服务总线设计
背景 基于总线的设计,借鉴了计算机内部硬件组成的设计思想(通过总线传输数据).在分布式系统中,不同子系统之间需要实现相互通信和远程调用,比较直接的方式就是“点对点”的通信方式,但是这样会暴露出一些很明 ...
- 在k8s上部署第一个php应用
一.搭建nginx+php 1.站点配置文件 1.1创建nginx-configmap.yaml [root@master k8s]# cat nginx-configmap.yaml apiVers ...
- HTML-HTML5+CSS3权威指南阅读(三、CSS3)
不同的浏览器(包括-moz-代表的Mozilla Firefox, -ms-代表的Microsoft Internet Explorer等)厂商在发布正式版本之前之前, 试验各自对CSS3新特性的实现 ...