C# 生成100个不重复的8位字符[a-z0-9],深圳亚鼎的一面试问题

mychinabc 2010-10-13 10:59:51
当时没做出来,现在想了想,这样能写出来,不知道其它大虾还有更好的方法不?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RandomTest
{
class Program
{
static void Main(string[] args)
{
List<string> aRandomList=new List<string>();

for (int i = 0; i < 100; )
{
string stemp = GetRandom();
if (aRandomList.Contains(stemp))
continue;
else
{
aRandomList.Add(stemp);
i++;
}
}

Console.Read();
}

static string GetRandom()
{
string words = "abcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder oSB = new StringBuilder();
Random r = new Random();

for (int i = 0; i < 8; i++)
{
string word = words.Substring(r.Next(words.Length - 1), 1);
oSB.Append(word);
}
string sTemp = oSB.ToString();
return sTemp;
}
}
}

...全文
615 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sky285402094 2011-04-30
  • 打赏
  • 举报
回复
用 list<> 随机生成我还没试过呢。。。
qq542831317 2011-04-30
  • 打赏
  • 举报
回复
随机出来一个数之后,在本次循环里把他在总数组里减去,这样一次次的就肯定不会得到重复的啊!
sxldfang 2011-04-29
  • 打赏
  • 举报
回复
也来个,别拍我!


static void Main(string[] args)
{
for (int i = 10000000; i < 10000100; ++i)
{
Console.Write("{0}\n", i.ToString());
}
Console.ReadKey();
}
秋的红果实 2011-04-29
  • 打赏
  • 举报
回复
总共36个字符,规模不大,移动数组元素不费劲,那么可以这样:弄个辅助数组,取一个,把原数组里剩下的元素放到辅助数组,再从辅助数组里任意取一个,...递归
wanghui0380 2011-04-29
  • 打赏
  • 举报
回复
或者
System.IO.Path.GetTempFileName()
wanghui0380 2011-04-29
  • 打赏
  • 举报
回复
呵呵,Path.GetRandomFileName()
jimh 2011-04-29
  • 打赏
  • 举报
回复
Guid最大的问题是只有0-9,a-f,一共16个值,而题目要求是0-9,a-z,相差太远了。
dream1889 2011-04-29
  • 打赏
  • 举报
回复
学习了,guid的出来的字符就是在[0-9][a-z]中取的么?没有[A-Z]和特殊字符 Right?
hwbox 2011-04-29
  • 打赏
  • 举报
回复

private void a()
{
char[] arr = "1234567890abcdefghijklmnopqrstuvwxyz".ToCharArray();
Random rand = new Random();

Dictionary<string,string> strs = new Dictionary<string,string>();

int i = 0;
while (i<100)
{
bool flag2 = true;
string temp = "";
for(int j = 0;j<8;j++)
{
temp += arr[rand.Next(0, arr.Length)];
}
if(!strs.ContainsKey(temp))
{
strs.Add(temp,"");
i++;
}
}

foreach(KeyValuePair<string,string> item in strs)
{
Response.Write(item.Key);
Response.Write("<br/>");
}
}

davidcoffee 2011-04-29
  • 打赏
  • 举报
回复
路过学习+帮顶~测试了用Guid的方法不会出现重复值:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RandomWords
{
class Program
{
static void Main(string[] args)
{
CheckIsExist(GenerateRandomWord());
Console.ReadLine();
}

/// <summary>
/// 得到随即8位字符串
/// </summary>
/// <returns></returns>
private static List<string> GenerateRandomWord()
{
string resultString;
List<string> resultList = new List<string>();
for (int i = 0; i < 100; i++)
{
Guid guidString = Guid.NewGuid();
resultString = guidString.ToString().Substring(0, 8);
Console.WriteLine(resultString);
resultList.Add(resultString);
}
return resultList;
}

/// <summary>
/// 判断列表中是否存在重复元素
/// </summary>
/// <param name="targetList"></param>
private static void CheckIsExist(List<string> targetList)
{
List<string> repeatList = new List<string>();
for (int i = 0; i < targetList.Count; i++)
{
var result = from target in targetList
where target == targetList[i]
select target;
if (result.Count() > 1)
{
repeatList.Add(targetList[i]);
}
}
bool flag = repeatList.Count == 0 ? false : true;
if (!flag)
{
Console.WriteLine("没有重复值");
}
else
{
Console.WriteLine("存在以下重复值");
foreach (var item in repeatList)
{
Console.WriteLine(item);
}
}
}
}
}
礼拜六 2011-04-29
  • 打赏
  • 举报
回复
Contains(object item)
changjiangzhibin 2011-04-29
  • 打赏
  • 举报
回复
使用DateTime Ticket也可以的,主要是控制随机种子
  • 打赏
  • 举报
回复
面试中从一个人写的代码可以看出他的水平,估计楼主还要考虑效率什么的
----我们老师,上次去当过面试官的说。。。
  • 打赏
  • 举报
回复
在各种泛型集合中,HashSet对于查找来说是最快的
  • 打赏
  • 举报
回复
真的郁闷死了,你这道面试题是我们老师前些天布置的一个实验。。
salens 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 pthiiu 的回复:]
引用 20 楼 anakin_skywalker 的回复:

guid本质就是伪随机,你可以写个简化版的伪随机序列,杀鸡焉用牛刀,每生成一次就检查是否重复了,如果重复就不要,继续取够100个


我也是这儿想的
[/Quote]
GUID,何必如此用?
李先生2017 2010-10-15
  • 打赏
  • 举报
回复
 ArrayList result = new ArrayList();
for (int i = 0; i < 100; i++)
{
result.Add( System.Guid.NewGuid().ToString().Substring(0, 8)) ;
}
ztenv 2010-10-15
  • 打赏
  • 举报
回复
用guid,真是小把戏,出题的公司也挺那个的吧?考这种题,
pthiiu 2010-10-15
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
int NUM = 100;

List<string> list = new List<string>();
string s;
while(list.Count<100)
{
s = Guid.NewGuid().ToString().Substring(0, 8);
if (ss.Contains(s))
continue;
list.Add(s);
}
}
pthiiu 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 anakin_skywalker 的回复:]

guid本质就是伪随机,你可以写个简化版的伪随机序列,杀鸡焉用牛刀,每生成一次就检查是否重复了,如果重复就不要,继续取够100个
[/Quote]

我也是这儿想的
加载更多回复(20)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧