36选7中生成随机数!!
最近,到外面吃饭了。路过一间福利彩票销售点,心血来潮,买了一张,但不知道买什么号码了,最后就机选了,晚上一开,2元钱又泡汤了。。哈哈~~!!!回家想了一想,想自己开发一个小程序,自动选号的。心想不就是一个随机函数吧,一试。。怎么CPU占用率会100%,原来凭我大一,一年学习C#的知识是不够,最终自己都不知道是什么问题。。。郁闷~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~源代码~~~~~~~~~~~~~~~~~~~~~~~~~~
System.Random ran = new Random();
int[] arry=new int[7];
for (int i = 0; i <arry.Length; i++)
{
where: arry[i]=(int)ran.Next(0,36)+1;
for (int j = 0; j <= i;j++ )
{
if (arry[i] == arry[j])
goto where;
}
}
textBox1.Text=arry[1]+"";
textBox2.Text = arry[2] + "";
textBox6.Text = arry[3] + "";
textBox4.Text = arry[4] + "";
textBox5.Text = arry[5] + "";
textBox6.Text = arry[6] + "";
textBox7.Text = arry[7] + "";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
各位兄弟姐妹请支招~~~~~~~~~~谢谢!!!
--------------------编程问答-------------------- System.Random ran = new Random();
int[] arry = new int[7];
for (int i = 0; i < arry.Length; i++)
{
//where:
arry[i] = (int)ran.Next(0, 36) + 1;
//此处内循环多余,去看一下Random类的解释
//for (int j = 0; j <= i; j++)
//{
// if (arry[i] == arry[j])
// goto where;
//}
}
textBox1.Text = arry[1] + "";
textBox2.Text = arry[2] + "";
textBox6.Text = arry[3] + "";
textBox4.Text = arry[4] + "";
textBox5.Text = arry[5] + "";
textBox6.Text = arry[6] + "";
textBox7.Text = arry[7] + ""; //数组越界错误 --------------------编程问答-------------------- ding... --------------------编程问答-------------------- 1楼的不对,是有可能出现重复的,必须得比对 --------------------编程问答-------------------- 把1到36存储在类型为arraylist的变量(M)中,然后每次从M中随机取出一个,把取出的数从M中移除(这样避免重复取出)循环7次
--------------------编程问答--------------------
//算法
public ArrayList PickNfromM(ArrayList m, int n)
{
ArrayList result = new ArrayList();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < n; i++)
{
int index = rnd.Next(m.Count);
result.Add(m[index]);
m.RemoveAt(index);
}
return result;
}
//调用
private void button1_Click(object sender, EventArgs e)
{
ArrayList al = new ArrayList();
for (int i = 1; i <=36; i++)
{
al.Add(i);
}
al = PickNfromM(al, 7);
listBox1.Items.Clear();
for (int i = 0; i < al.Count; i++)
{
listBox1.Items.Add(al[i].ToString());
}
}
--------------------编程问答-------------------- 正好有点时间,实现了一个完整代码
int[] arry = new int[7];
for (int i = 0; i < arry.Length; i++)
{
System.Random ran = new Random();
arry[i] = (int)ran.Next(0, 36) + 1;
for (int j = 0; j < i; j++)
{
while (arry[i] == arry[j])
{
arry[i] = ran.Next(0, 36) + 1;
j = 0;
}
}
}
--------------------编程问答-------------------- up --------------------编程问答-------------------- --------------------编程问答-------------------- 楼主的头像太暴力了。 --------------------编程问答-------------------- 刚看到你的软件标题。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Generater g = new Generater(36, 7);
foreach (int i in g.Generate())
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.Read();
}
}
class Generater
{
Random rnd;
int Total, Choose;
/// <summary>
/// ctor
/// </summary>
/// <param name="Total">总数</param>
/// <param name="Choose">要选择的个数</param>
public Generater(int Total, int Choose)
{
this.Total = Total;
this.Choose = Choose;
rnd = new Random(DateTime.Now.Millisecond);
}
public int[] Generate() //生成随机选择
{
int[] Result = new int[Choose];
int CurTotal = Total, CurChoose = Choose;
for (int i = 1; i <= Total && CurChoose > 0; i++) //从1开始依次检测。如果已经全了就不继续了
{
if (TestRate(CurChoose, CurTotal))
{
CurChoose--;
Result[CurChoose] = i;
}
CurTotal--;
}
Array.Reverse(Result);
return Result;
}
private bool TestRate(int CurChoose, int CurTotal) //随机检测 CurChoose/CurTotal 概率下事件是否发生
{
if (CurChoose >= CurTotal)
{
return true;
}
double d = rnd.NextDouble();
return d * CurTotal < CurChoose;
}
}
}
你这个系统100万买吗? --------------------编程问答-------------------- mark!!! --------------------编程问答--------------------
100万太少了,中一注都500万了。。。哈哈 --------------------编程问答-------------------- 买彩票...还打算长期买...还是福彩....太可悲了 --------------------编程问答-------------------- 楼上别太认真。娱乐一下也无防。
我和我女朋友,心情好时也买。 --------------------编程问答-------------------- 呵呵!楼主真是NB啊! --------------------编程问答-------------------- 福利事业需要你。 --------------------编程问答-------------------- 六楼的厉害……
补充:.NET技术 , C#