展开全部
简单用法:
Random rd = new Random();
MSDN
rd .Next (Int32, Int32) 返回一个指定范围内的随机数。 由 .NET Compact Framework 支持。
比如:rd.Next(3,100) 返回3到100的随机银陪数
Random类源码:
// ==++==
//
//
// Copyright (c) 2002 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the file
// named license.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
//
// ==--==
/**//*============================================================
**
** Class: Random.cs
**
**
**
** Purpose: A random number generator.
**
** Date: July 8, 1998
**
===========================================================*/
namespace System {
using System;
using System.Runtime.CompilerServices;
/**//// <include file='doc\Random.uex' path='docs/亏搏档doc[@for="Random"]/*' />
[Serializable()] public class Random {
//
// Private Constants
//
private const int MBIG = Int32.MaxValue;
private const int MSEED = 161803398;
private const int MZ = 0;
//
// Member Variables
//
private int inext, inextp;
private int[] SeedArray = new int[56];
//
// Public Constants
//
//
// Native Declarations
//
//
// Constructors
//
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Random"]/*' />
public Random()
: this(Environment.TickCount) {
}
/**//// <include file='销乱doc\Random.uex' path='docs/doc[@for="Random.Random1"]/*' />
public Random(int Seed) {
int ii;
int mj, mk;
//Initialize our Seed array.
//This algorithm comes from Numerical Recipes in C (2nd Ed.)
mj = MSEED - Math.Abs(Seed);
SeedArray[55]=mj;
mk=1;
for (int i=1; i<55; i++) { //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
ii = (21*i)%55;
SeedArray[ii]=mk;
mk = mj - mk;
if (mk<0) mk+=MBIG;
mj=SeedArray[ii];
}
for (int k=1; k<5; k++) {
for (int i=1; i<56; i++) {
SeedArray[i] -= SeedArray[1+(i+30)%55];
if (SeedArray[i]<0) SeedArray[i]+=MBIG;
}
}
inext=0;
inextp = 21;
Seed = 1;
}
//
// Package Private Methods
//
/**//*====================================Sample====================================
**Action: Return a new random number [0..1) and reSeed the Seed array.
**Returns: A double [0..1)
**Arguments: None
**Exceptions: None
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Sample"]/*' />
protected virtual double Sample() {
int retVal;
int locINext = inext;
int locINextp = inextp;
if (++locINext >=56) locINext=1;
if (++locINextp>= 56) locINextp = 1;
retVal = SeedArray[locINext]-SeedArray[locINextp];
if (retVal<0) retVal+=MBIG;
SeedArray[locINext]=retVal;
inext = locINext;
inextp = locINextp;
//Including this division at the end gives us significantly improved
//random number distribution.
return (retVal*(1.0/MBIG));
}
//
// Public Instance Methods
//
/**//*=====================================Next=====================================
**Returns: An int [0.._int4.MaxValue)
**Arguments: None
**Exceptions: None.
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next"]/*' />
public virtual int Next() {
return (int)(Sample()*Int32.MaxValue);
}
/**//*=====================================Next=====================================
**Returns: An int [minvalue..maxvalue)
**Arguments: minValue -- the least legal value for the Random number.
** maxValue -- the greatest legal return value.
**Exceptions: None.
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next1"]/*' />
public virtual int Next(int minValue, int maxValue) {
if (minValue>maxValue) {
throw new ArgumentOutOfRangeException("minValue",String.Format(Environment.GetResourceString("Argument_MinMaxValue"), "minValue", "maxValue"));
}
int range = (maxValue-minValue);
//This is the case where we flipped around (e.g. MaxValue-MinValue);
if (range<0) {
long longRange = (long)maxValue-(long)minValue;
return (int)(((long)(Sample()*((double)longRange)))+minValue);
}
return ((int)(Sample()*(range)))+minValue;
}
/**//*=====================================Next=====================================
**Returns: An int [0..maxValue)
**Arguments: maxValue -- the greatest legal return value.
**Exceptions: None.
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next2"]/*' />
public virtual int Next(int maxValue) {
if (maxValue<0) {
throw new ArgumentOutOfRangeException("maxValue", String.Format(Environment.GetResourceString("ArgumentOutOfRange_MustBePositive"), "maxValue"));
}
return (int)(Sample()*maxValue);
}
/**//*=====================================Next=====================================
**Returns: A double [0..1)
**Arguments: None
**Exceptions: None
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.NextDouble"]/*' />
public virtual double NextDouble() {
return Sample();
}
/**//*==================================NextBytes===================================
**Action: Fills the byte array with random bytes [0..0x7f]. The entire array is filled.
**Returns:Void
**Arugments: buffer -- the array to be filled.
**Exceptions: None
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.NextBytes"]/*' />
public virtual void NextBytes(byte [] buffer){
if (buffer==null) throw new ArgumentNullException("buffer");
for (int i=0; i<buffer.Length; i++) {
buffer[i]=(byte)(Sample()*(Byte.MaxValue+1));
}
}
}
}
Random rd = new Random();
MSDN
rd .Next (Int32, Int32) 返回一个指定范围内的随机数。 由 .NET Compact Framework 支持。
比如:rd.Next(3,100) 返回3到100的随机银陪数
Random类源码:
// ==++==
//
//
// Copyright (c) 2002 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the file
// named license.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
//
// ==--==
/**//*============================================================
**
** Class: Random.cs
**
**
**
** Purpose: A random number generator.
**
** Date: July 8, 1998
**
===========================================================*/
namespace System {
using System;
using System.Runtime.CompilerServices;
/**//// <include file='doc\Random.uex' path='docs/亏搏档doc[@for="Random"]/*' />
[Serializable()] public class Random {
//
// Private Constants
//
private const int MBIG = Int32.MaxValue;
private const int MSEED = 161803398;
private const int MZ = 0;
//
// Member Variables
//
private int inext, inextp;
private int[] SeedArray = new int[56];
//
// Public Constants
//
//
// Native Declarations
//
//
// Constructors
//
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Random"]/*' />
public Random()
: this(Environment.TickCount) {
}
/**//// <include file='销乱doc\Random.uex' path='docs/doc[@for="Random.Random1"]/*' />
public Random(int Seed) {
int ii;
int mj, mk;
//Initialize our Seed array.
//This algorithm comes from Numerical Recipes in C (2nd Ed.)
mj = MSEED - Math.Abs(Seed);
SeedArray[55]=mj;
mk=1;
for (int i=1; i<55; i++) { //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
ii = (21*i)%55;
SeedArray[ii]=mk;
mk = mj - mk;
if (mk<0) mk+=MBIG;
mj=SeedArray[ii];
}
for (int k=1; k<5; k++) {
for (int i=1; i<56; i++) {
SeedArray[i] -= SeedArray[1+(i+30)%55];
if (SeedArray[i]<0) SeedArray[i]+=MBIG;
}
}
inext=0;
inextp = 21;
Seed = 1;
}
//
// Package Private Methods
//
/**//*====================================Sample====================================
**Action: Return a new random number [0..1) and reSeed the Seed array.
**Returns: A double [0..1)
**Arguments: None
**Exceptions: None
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Sample"]/*' />
protected virtual double Sample() {
int retVal;
int locINext = inext;
int locINextp = inextp;
if (++locINext >=56) locINext=1;
if (++locINextp>= 56) locINextp = 1;
retVal = SeedArray[locINext]-SeedArray[locINextp];
if (retVal<0) retVal+=MBIG;
SeedArray[locINext]=retVal;
inext = locINext;
inextp = locINextp;
//Including this division at the end gives us significantly improved
//random number distribution.
return (retVal*(1.0/MBIG));
}
//
// Public Instance Methods
//
/**//*=====================================Next=====================================
**Returns: An int [0.._int4.MaxValue)
**Arguments: None
**Exceptions: None.
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next"]/*' />
public virtual int Next() {
return (int)(Sample()*Int32.MaxValue);
}
/**//*=====================================Next=====================================
**Returns: An int [minvalue..maxvalue)
**Arguments: minValue -- the least legal value for the Random number.
** maxValue -- the greatest legal return value.
**Exceptions: None.
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next1"]/*' />
public virtual int Next(int minValue, int maxValue) {
if (minValue>maxValue) {
throw new ArgumentOutOfRangeException("minValue",String.Format(Environment.GetResourceString("Argument_MinMaxValue"), "minValue", "maxValue"));
}
int range = (maxValue-minValue);
//This is the case where we flipped around (e.g. MaxValue-MinValue);
if (range<0) {
long longRange = (long)maxValue-(long)minValue;
return (int)(((long)(Sample()*((double)longRange)))+minValue);
}
return ((int)(Sample()*(range)))+minValue;
}
/**//*=====================================Next=====================================
**Returns: An int [0..maxValue)
**Arguments: maxValue -- the greatest legal return value.
**Exceptions: None.
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.Next2"]/*' />
public virtual int Next(int maxValue) {
if (maxValue<0) {
throw new ArgumentOutOfRangeException("maxValue", String.Format(Environment.GetResourceString("ArgumentOutOfRange_MustBePositive"), "maxValue"));
}
return (int)(Sample()*maxValue);
}
/**//*=====================================Next=====================================
**Returns: A double [0..1)
**Arguments: None
**Exceptions: None
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.NextDouble"]/*' />
public virtual double NextDouble() {
return Sample();
}
/**//*==================================NextBytes===================================
**Action: Fills the byte array with random bytes [0..0x7f]. The entire array is filled.
**Returns:Void
**Arugments: buffer -- the array to be filled.
**Exceptions: None
==============================================================================*/
/**//// <include file='doc\Random.uex' path='docs/doc[@for="Random.NextBytes"]/*' />
public virtual void NextBytes(byte [] buffer){
if (buffer==null) throw new ArgumentNullException("buffer");
for (int i=0; i<buffer.Length; i++) {
buffer[i]=(byte)(Sample()*(Byte.MaxValue+1));
}
}
}
}
展开全部
验蔽袜态证码:好蠢
后台:宏源
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
namespace SecurMass
{
public partial class YZMnum2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
Session["CheckCode"] = checkCode;
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 20);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
}
后台:宏源
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
namespace SecurMass
{
public partial class YZMnum2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
Session["CheckCode"] = checkCode;
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 20);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
random rmd=new random();
Label1.Text=rmd.Next(1,9).ToString();就会在1到首辩9中取一个数拦厅字
Label2.Text=((char)(rmd.Next(0, 26) + 65)).ToString();大写字母者衡缺
Label3.Text=((char)(rmd.Next(0, 26) + 96)).ToString();小写字母
Label1.Text=rmd.Next(1,9).ToString();就会在1到首辩9中取一个数拦厅字
Label2.Text=((char)(rmd.Next(0, 26) + 65)).ToString();大写字母者衡缺
Label3.Text=((char)(rmd.Next(0, 26) + 96)).ToString();小写字母
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询