Đáp:
Chúng tôi cung cấp đến bạn đoạn code Tạo số ngẫu nhiên trong asp.net để bạn tham khảo như sau:
/// <summary>
/// Get a Random object which is cached between requests.
/// The advantage over creating the object locally is that the .Next
/// call will always return a new value. If creating several times locally
/// with a generated seed (like millisecond ticks), the same number can be
/// returned.
/// </summary>
/// <returns>A Random object which is cached between calls.</returns>
public static Random GetRandom(int seed)
{
Random r = (Random)HttpContext.Current.Cache.Get("RandomNumber");
if (r == null) {
if (seed == 0)
r = new Random();
else r = new Random(seed);
HttpContext.Current.Cache.Insert("RandomNumber",r);
}
return r;
}
/// <summary>
/// GetRandom with no parameters.
/// </summary>
/// <returns>A Random object which is cached between calls.</returns>
public static Random GetRandom()
{
return GetRandom(0);
}
Gọi trong code:
Random r = GetRandom();
lock (r)
{
String myNumber = r.Next(0,99999).ToString("00000"); }
kgơagà
Chúc bạn thành công!
3c sẵn sàng mang đến bạn sự hỗ trợ tốt nhất
--------------------------------------------------------------------------------------------------------------------
3C Software Co.
Office: No 6 Lang Ha - Dong Da - Ha Noi - Viet Nam
(Office : (84) 4-2665656
ÊFax : (84) 4-2665657
*E-mail : webmaster@3c.com.vn
üWebsite: www.3c.com.vn