110,534
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
}
static void Main(string[] args)
{
for (int i = 10000000; i < 10000100; ++i)
{
Console.Write("{0}\n", i.ToString());
}
Console.ReadKey();
}
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/>");
}
}
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);
}
}
}
}
}
ArrayList result = new ArrayList();
for (int i = 0; i < 100; i++)
{
result.Add( System.Guid.NewGuid().ToString().Substring(0, 8)) ;
}
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);
}
}