作者F23ko (纯洁)
看板C_Sharp
标题Re: [问题] 乱数产生的方式..
时间Sun Feb 28 03:53:25 2010
觉得很有趣,就试着写写看
http://www.badongo.com/file/20861796
自己跑的时间约是8秒左右
===================================================================
由於十六进位的数字有20位
16^20 = 1208925819614629174706176
decimal范围 从 正 79228162514264337593543950335
到 负 79228162514264337593543950335
也就是说,用一个decima可以接受的了20位HEX乱数的范围
所以我就用三个int组成一个decimal下去做计算
decimal a, b, c;
decimal temp;
a = Rm乱数.Next(1208925);
b = Rm乱数.Next(999999999);
c = Rm乱数.Next(999999999);
temp = (a * 1000000000000000000M + b * 1000000000M + c);
另外以自己在读的书上的二元树范例为样本,做个
不重覆二元树(简称:不元树)
稍微修改了部分:
加入新数字时,会检查是否重覆,传回一个bool值
如果重覆,就不会写入,并传回ture
并且,如果产生乱数的主回圈,收到不元树传回ture的话
就把把回圈的i--重来
所以应该是可以回避掉重覆的机率
另外,在遇到重覆、超出最大值时都会丢出字串。
但除了故意设定非法树值去测试之外
我还没遇到过重覆或是超过最大值的状况....
这机率真的很低
以下原始码:
==================================================================
不过我不会把decimal转成HEX.....
就没写.....
有人能教一下吗?不元树.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 不重覆之二元树
{
public class 不元树<树节> : IEnumerable<树节>
where 树节 : IComparable<树节>
{
public 树节 节点资料 { get; set; }
public 不元树<树节> 大方 { get; set; }
public 不元树<树节> 小方 { get; set; }
private static int 资料数目in = 1;
public static int 资料数目
{
get
{
return 资料数目in;
}
}
public 不元树(树节 输入值)
{
this.节点资料 = 输入值;
this.大方 = null;
this.小方 = null;
}
public bool 插入(树节 新物件)
{
树节 比对物 = this.节点资料;
int 比较大小 = 比对物.CompareTo(新物件);
if (比较大小 > 0)
{
if (this.大方 == null)
{
this.大方 = new 不元树<树节>(新物件);
资料数目in++;
return false;
}
else
{
return this.大方.插入(新物件);
}
}
else if (比较大小 < 0)
{
if (this.小方 == null)
{
this.小方 = new 不元树<树节>(新物件);
资料数目in++;
return false;
}
else
{
return this.小方.插入(新物件);
}
}
else if (比较大小 == 0)
{
return true;
}
else throw new Exception("加入档案时发生不明错误");
}
#region IEnumerable<TItem> Members
IEnumerator<树节> IEnumerable<树节>.GetEnumerator()
{
if (this.大方 != null)
{
foreach (树节 物件 in this.大方)
{
yield return 物件;
}
}
yield return this.节点资料;
if (this.小方 != null)
{
foreach (树节 物件 in this.小方)
{
yield return 物件;
}
}
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
}
==================================================================
核心.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 不重覆之二元树;
using System.IO;
namespace 核心执行区
{
class 核心
{
static void Main(string[] args)
{
DateTime 起始时间 = DateTime.Now;
生产乱数 乱数 = new 生产乱数();
乱数.开始();
Console.WriteLine("写入完成,乱数放在「乱数文件.txt」当中");
string 时间 = (DateTime.Now - 起始时间).ToString();
Console.WriteLine("经历时间:{0} 秒", 时间);
Console.ReadKey();
}
class 生产乱数
{
Random Rm乱数 = new Random();
public 生产乱数()
{
}
public void 开始()
{
using (StreamWriter 写入 = new StreamWriter("乱数文件.txt"))
{
//宣告接下来会用的变数
decimal a, b, c;
decimal temp;
//初始化不元树
Restart:
a = Rm乱数.Next(1208925);
b = Rm乱数.Next(999999999);
c = Rm乱数.Next(999999999);
temp = (a * 1000000000000000000M + b * 1000000000M + c);
if (temp > 1208925819614629174706176M) goto Restart;
不元树<decimal> 不元树仓库 = new 不元树<decimal>(temp);
写入.WriteLine(temp);
for (int i = 1; i <= 1000000; i++)
{
a = Rm乱数.Next(1208925);
b = Rm乱数.Next(999999999);
c = Rm乱数.Next(999999999);
temp
= (a * 1000000000000000000M + b * 1000000000M + c);
//检查数字是否合法
if (temp > 1208925819614629174706176M)
{
Console.WriteLine("数字不合法於:{0}",temp);
i--;
continue;
}
//写入不元树,如果收到写入失败就重来
//(重覆的话会传会true)
if (不元树仓库.插入(temp))
{
Console.WriteLine("数字重覆於:{0}", temp);
i--;
continue;
}
写入.WriteLine(temp);
}
}
}
}
}
}
====================================================================
不过我不会把decimal转成HEX.....
就没写.....
有人能教一下吗?
--
◢
乡民啊!乡民! 请告诉我谁是最纯洁的人!
◢█ │
PTT │
██
:就是你!Snow White F23ko!│
█◤ ╯
◤ ﹨(╯▽╰ )∕
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 163.27.109.99
1F:→ F23ko:等一下.... FFFFFFFFFFFFFFFFFFFF换算成十进位我算的不太清 02/28 03:58
2F:→ F23ko:楚..... 最大值还要再确认过.... 02/28 03:59
3F:→ proach:直觉猜是 -1 02/28 13:31
4F:→ joefaq:推楼上 02/28 15:46
5F:→ F23ko:为什麽是-1? 02/28 16:04
6F:推 liaommx:tree..看到tree就快疯了XD.. 03/01 10:43
7F:→ liaommx:目前是用list的方法,list sort,现在在想办法找有没有办法l 03/01 10:44
8F:→ liaommx:不重复, 03/01 10:44
9F:→ hpo14:33, 35行的 true 打错了 03/01 21:22
10F:→ F23ko:那边不在程式片段中,错了没差 XD 03/01 21:25
11F:推 virdust2003:你真的用中文当变数名称? 03/02 07:11
12F:→ F23ko:真的... 03/02 10:17