如何用c#从一组数中php 从数组随机抽取取数字?

C#产生指定范围随机数的几种方法-亮术网
C#产生指定范围随机数的几种方法
  在开发过程中,常常要产生随机数,如生成静态 html 网页时,文件名通常用产生随机数的方式获得,生成定单的时候,定单号也可以用产生随机数的方式获得等。
  在 C# 中,一般都用 Random 产生随机数,它可任意指定产生随机数范围。Random 结合数组,可以产生一些特殊范围的随机数,以满足特殊的需要。
  一、用 Random 产生指定范围随机数
  1、产生指定上限的随机数(如产生100以内的随机数)
  Random ran = new Random();
  int n = ran.Next(100);
  2、产生指定上下限的随机数(如产生100到1000的随机数)
  Random ran = new Random();
  int n = ran.Next(100, 1000);
  二、用 Random 结合数组产生指定范围随机数
  在某些情况下,随机数只能取一些特殊指定的值,如不连续的数字或指定的一些单词等,此时仅用 Random 无法达到要求,必须借住数组才能实现。实现思路大概是这样:先把这些特殊的值存到数组中,然后把数组的长度作为 Random 的上限产生随机数,此随机数正是数组的下标,根据该下标取得数组的值。
  1、示例一
  假如要产生不连续的随机数,代码如下:
  public string GetRandom(string[] arr)
    Random ran = new Random();
    int n = ran.Next(arr.Length - 1);
    return arr[n];
  调用方法:
  string[] arr = { &25&, &28&, &30&, &50&, &60& };
  GetRandom(arr);
  2、示例二
  假如要用指定单词作为随机数的取值,代码实现跟示例一相同,所不同的仅是随机数的取值,所以只要定义一个单词数组直接调用上面代码即可。
  调用方法:
  string[] arr = { &red&, &green&, &blue&, &orange&, &white& };
  GetRandom(arr);
  以上两种产生指定随机数的方法,都通过测试,可根据实际开发需要灵活选择,一般情况都是直接用 Random 就可以了。温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
& & & & /// 提取字符串中的第一个数字字符串,多个数字相邻时视为一个字符串& & &&& & & & public static bool ExtraFirstNum(string str, ref string strNumPre, ref string strNum, ref string strAfter)& & & & {& & & & & & strNumPre = string.E& & & & & & strNum = string.E& & & & & & strAfter = string.E& & & & & & bool bFindNum =& & & & & & for (int i = 0; i & str.L i++)& & & & & & {& & & & & & & & if (Char.IsNumber(str, i) == true)& & & & & & & & {& & & & & & & & & & strNum += str.Substring(i, 1);& & & & & & & & & & bFindNum =& & & & & & & & }& & & & & & & & else& & & & & & & & {& & & & & & & & & &if (bFindNum)& & & & & & & & & &{& & & & & & & & & & & &strAfter = str.Substring(i, str.Length - i);& & & & & & & & & & & && & & & & & & & & &}& & & & & & & & & &else& & & & & & & & & &{& & & & & & & & & & & &strNumPre += str.Substring(i, 1); & & & & & & & & & & &&& & & & & & & & & &}& & & & & & & & }& & & & & & }& & & & & && & & & & & return bFindN& & & & }
阅读(3184)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'C#提取字符串中的第一个数字字符串',
blogAbstract:'// &summary&& & & & /// 提取字符串中的第一个数字字符串,多个数字相邻时视为一个字符串& & &&& & & & public static bool ExtraFirstNum(string str, ref string strNumPre, ref string strNum, ref string strAfter)& & & & {& & & & & & strNumPre = string.E& & & & & & strNum = string.E& & & & & & strAfter = string.E',
blogTag:'c#,提取,数字,字符串',
blogUrl:'blog/static/1',
isPublished:1,
istop:false,
modifyTime:5,
publishTime:1,
permalink:'blog/static/1',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}扫二维码下载作业帮
2亿+学生的选择
下载作业帮安装包
扫二维码下载作业帮
2亿+学生的选择
如何在C#中,数字的排列组合!比如123,1代表在123中选1个数,2代表在456中选2个数,3代表在789中选3个数如何在C#中,在testbox随便输入3位的数字,比如123,点击确定,如何实现功能,1代表在123中选1个数字,2代表在456中选2个数字,3代表在789中选3个数字,然后随机组合,组合成为一个6位数.将所有结果显示!大侠们,求救!上面写错了,比如输入321,才是这种情况!1代表在1,2,3中选1个数字,2代表在4,5,6中选2个数字,3代表在7,8,9中选3个数字.这是三组数字!1,2,3是一组,4,5,6是一组,7,8,9是一组!最后不是一共选出6个数字么?然后随机排列6位数,要求所有的组合都显示出来!
换行符248uJ
扫二维码下载作业帮
2亿+学生的选择
string a=this.testbox.text.substring(0,1);//第一位string b=this.testbox.text.substring(1,1);//第2位string c=this.testbox.text.substring(2,1);//第3位看不懂你要的是什么效果,可以用随机数再进行组合
为您推荐:
其他类似问题
扫描下载二维码其他回答(1)
using Susing System.Dusing System.Cusing System.Cusing System.Wusing System.Web.Susing System.Web.UI;using System.Web.UI.WebCusing System.Web.UI.WebControls.WebPusing System.Web.UI.HtmlCusing System.D
public partial class Default2 : System.Web.UI.Page{&&& protected void Page_Load(object sender, EventArgs e)&&& {&&&&&&& this.CreateCheckCodeImage(GenerateCheckCode());&&& }
&&& //产生随机数的函数&&& private string GenerateCheckCode()&&& {&&&&&&&&&&&&&&&&&&&&& //空字符串且为只读属性&&&&&&& string checkCode = String.E&&&&&&& //新建一个随机数产生器&&&&&&& 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&&&&&&&&&&&&&&& //如果随机数是奇数 选择从[A-Z]&&&&&&&&&&&&&&& code = (char)('A' + (char)(number % 26));&&&&&&&&&&& //5个字符的组合&&&&&&&&&&& checkCode += code.ToString();&&&&&&& }&&&&&&& //将字符串checkCode写入Cookies并且返回字符串checkCode&&&&&&& Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));&&&&&&& return checkC&&& }&&& //建立一个随机图形&&& private void CreateCheckCodeImage(string checkCode)&&& {&&&&&&& if (checkCode == null || checkCode.Trim() == String.Empty)&&&&&&&&&&&&&&&&&& //建立一个位图文件 确立长宽&&&&&&& System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);&&&&&&& 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();&&&&&&& }&&& }
园豆:5757
&&&您需要以后才能回答,未注册用户请先。}

我要回帖

更多关于 在一组数字中随机抽取 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信