static void Main(string[] args)
{
Random randObj = new Random();
int A;
int B;
int C;
int D;
//int now;
for (int j = 0; j < 100; j++)
{
A = randObj.Next(0, 4);
B = randObj.Next(0, 4);
if (B != A)
{
C = randObj.Next(0, 4);
if (C != A && C != B)
{
D = randObj.Next(0, 4);
if (D != A && D != B && D != C)
{
Console.WriteLine(A + "#" + B + "#" + C + "#" + D);
break;
}
}
else
{
continue;
}
}
else
{
continue;
}
}
}
{
Random randObj = new Random();
int A;
int B;
int C;
int D;
//int now;
for (int j = 0; j < 100; j++)
{
A = randObj.Next(0, 4);
B = randObj.Next(0, 4);
if (B != A)
{
C = randObj.Next(0, 4);
if (C != A && C != B)
{
D = randObj.Next(0, 4);
if (D != A && D != B && D != C)
{
Console.WriteLine(A + "#" + B + "#" + C + "#" + D);
break;
}
}
else
{
continue;
}
}
else
{
continue;
}
}
}
int i = r.Next(3);
你的可能出现极值,也就是100次没取道,就取不了了
List<int> list = new List<int>();
for (int index = 0; index < 4; index++)
{
list.Add(index);
} StringBuilder sb = new StringBuilder();
Random random = new Random(); for (int index = 0; index < 4; index++)
{
int i = random.Next(0, list.Count); if (sb.Length == 0)
{
sb.Append(list[i]);
}
else
{
sb.Append("#" + list[i]);
} list.Remove(list[i]);
} MessageBox.Show(sb.ToString());
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Hashtable ht = new Hashtable(); a = GetRandom();
ht.Add(a, a);
while (true)
{
b = GetRandom();
if (!ht.ContainsValue(b))
{
ht.Add(b, b);
break;
}
}
while (true)
{
c = GetRandom();
if (!ht.ContainsValue(c))
{
ht.Add(c, c);
break;
}
} Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c); Console.ReadLine();
} private static int GetRandom()
{
return new Random(DateTime.Now.Millisecond).Next(0, 4);
}
}
}
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import javax.servlet.http.*;
public class CreateNumberAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
CreateNumberActionForm cform=(CreateNumberActionForm)form;
HttpSession session=request.getSession();
session.removeAttribute("message");
int digits=Integer.parseInt(cform.getDigts());
int secret_number=(int)(Math.random()*digits);
session.setAttribute("secret_number",String.valueOf(secret_number));
// throw new java.lang.UnsupportedOperationException(
// "Method $execute() not yet implemented.");
return mapping.findForward("guess");
}
}
这是java里的 C#里和这差不多 int secret_number=(int)(Math.random()*digits);
其实,这就是个洗牌的原理,4张牌分给4个人,把牌随机洗洗就发了ok.
{
int len = array.Length;
System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
T[] ret=new T[len];
Random rand = new Random();
int i = 0;
while (list.Count < len)
{
int iter = rand.Next(0, len);
if (!list.Contains(iter))
{
list.Add(iter);
ret[i] = array[iter];
i++;
}
}
return ret;
}
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>(); for (int i = 0; i < 4; i++)
{
list.Add(i);
} StringBuilder sb = new StringBuilder();
while (true)
{
if (list.Count == 1) // 到 D 了
{
break;
} int index = new Random().Next(0, list.Count);
sb.Append(list[index]);
list.RemoveAt(index);
} Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
}
arr=[0,..............................];
Array.prototype.Del=function(n)
{
if(n<0)
return this;
else
return this.slice(0,n).concat(this.slice(n+1,this.length));
};for(var i=0;i<3;i++)
{
var index = Math.floor(Math.random()*(arr.length-1));
。//就很简单了arr=arr.Del(index);
}