2009-10-12 97 views
0

我想从随机选取的背景图像(从4个图像中选择)出现为asp.net面板的背景图像。DIV上的随机背景图像

我遇到的问题是,在调试模式下单步执行代码时,代码的工作原理为。一旦您在网站上运行代码而不进行调试,所有图像都是相同的。它几乎就像随机数没有得到足够快的拾取。

usercontrol位于数据列表的内部。

的用户控件是这样的:

<asp:Panel ID="productPanel" CssClass="ProductItem" runat="server"> 

<div class="title" visible="false"> 
    <asp:HyperLink ID="hlProduct" runat="server" /> 
</div> 
<div class="picture"> 
    <asp:HyperLink ID="hlImageLink" runat="server" /> 
</div> 
<div class="description" visible="false"> 
    <asp:Literal runat="server" ID="lShortDescription"></asp:Literal> 
</div> 
<div class="addInfo" visible="false"> 
    <div class="prices"> 
     <asp:Label ID="lblOldPrice" runat="server" CssClass="oldproductPrice" /> 
     <br /> 
     <asp:Label ID="lblPrice" runat="server" CssClass="productPrice" /></div> 
    <div class="buttons"> 
     <asp:Button runat="server" ID="btnProductDetails" OnCommand="btnProductDetails_Click" 
      Text="Details" ValidationGroup="ProductDetails" CommandArgument='<%# Eval("ProductID") %>' 
      SkinID="ProductGridProductDetailButton" /><br /> 
     <asp:Button runat="server" ID="btnAddToCart" OnCommand="btnAddToCart_Click" Text="Add to cart" 
      ValidationGroup="ProductDetails" CommandArgument='<%# Eval("ProductID") %>' SkinID="ProductGridAddToCartButton" /> 
    </div> 
</div> 

和背后的代码是这样的:

protected void Page_Load(object sender, EventArgs e) 
    { 

      // Some code here to generate a random number between 0 & 3 
      System.Random RandNum = new System.Random(); 
      int myInt = RandNum.Next(4); 

      if (productPanel.BackImageUrl != null) 
      { 
       switch (myInt) 
       { 
        case 0: 
         productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame1.gif"; 
         break; 
        case 1: 
         productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame2.gif"; 
         break; 
        case 2: 
         productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame3.gif"; 
         break; 
        case 3: 
         productPanel.BackImageUrl = "../App_Themes/emmaharris/images/frame4.gif"; 
         break; 
       } 

      } 
      // End of new code to switch background images 

    } 

牛逼

回答

1

有时候,随机是不是真的随机...

乔恩斯基特对话题的好文章:Why am I getting the same numbers out of Random time and time again?

要直接引述乔恩曾告诉我一个时间:

伪随机数发生器(如 System.Random)实际上并不是随机的 - 它始终会产生相同的 结果序列当初始化 与相同的dat一个。用于初始化的数据是 是一个号码 ,称为种子。

最根本的问题是,当你 创建使用 参数构造函数随机的一个新实例(如 我们在这里所做的),它使用从“当前时间”采取 的种子。因此,如果 你连续快速地创建 随机的几个新的情况下,他们将 都具有相同的种子 - 的“当前时间” 只能更改一次为15ms(这 是计算一个永恒)的 计算机的想法。

通常要什么(假设你 不关心能够 重现精确的结果,你不 需要加密的安全随机 数发生器)就是在整个使用单一 随机的程序, 第一次使用它被初始化。 这听起来像你可以在某处使用一个 静态字段(作为 属性公开) - 基本上是一个单身。 不幸的是System.Random不是 线程安全 - 如果你从两个不同的线程调用它,你可能会得到 的问题(包括在两个线程中获得相同的 数字序列)。

这就是为什么我在 创建StaticRandom我的小事业工具箱 - 这是 基本上得到 随机数,使用随机和锁单 实例的线程安全的方式。请参阅 http://www.yoda.arachsys.com/csharp/miscutil/usage/staticrandom.html (快速示例)和 http://pobox.com/~skeet/csharp/miscutil (针对库本身)。

乔恩斯基特的其它实用程序随机数发生器

using System; 

namespace MiscUtil 
{ 
    /// <summary> 
    /// Thread-safe equivalent of System.Random, using just static methods. 
    /// If all you want is a source of random numbers, this is an easy class to 
    /// use. If you need to specify your own seeds (eg for reproducible sequences 
    /// of numbers), use System.Random. 
    /// </summary> 
    public static class StaticRandom 
    { 
     static Random random = new Random(); 
     static object myLock = new object(); 

     /// <summary> 
     /// Returns a nonnegative random number. 
     /// </summary>  
     /// <returns>A 32-bit signed integer greater than or equal to zero and less than Int32.MaxValue.</returns> 
     public static int Next() 
     { 
      lock (myLock) 
      { 
       return random.Next(); 
      } 
     } 

     /// <summary> 
     /// Returns a nonnegative random number less than the specified maximum. 
     /// </summary> 
     /// <returns> 
     /// A 32-bit signed integer greater than or equal to zero, and less than maxValue; 
     /// that is, the range of return values includes zero but not maxValue. 
     /// </returns> 
     /// <exception cref="ArgumentOutOfRangeException">maxValue is less than zero.</exception> 
     public static int Next(int max) 
     { 
      lock (myLock) 
      { 
       return random.Next(max); 
      } 
     } 

     /// <summary> 
     /// Returns a random number within a specified range. 
     /// </summary> 
     /// <param name="min">The inclusive lower bound of the random number returned. </param> 
     /// <param name="max"> 
     /// The exclusive upper bound of the random number returned. 
     /// maxValue must be greater than or equal to minValue. 
     /// </param> 
     /// <returns> 
     /// A 32-bit signed integer greater than or equal to minValue and less than maxValue; 
     /// that is, the range of return values includes minValue but not maxValue. 
     /// If minValue equals maxValue, minValue is returned. 
     /// </returns> 
     /// <exception cref="ArgumentOutOfRangeException">minValue is greater than maxValue.</exception> 
     public static int Next(int min, int max) 
     { 
      lock (myLock) 
      { 
       return random.Next(min, max); 
      } 
     } 

     /// <summary> 
     /// Returns a random number between 0.0 and 1.0. 
     /// </summary> 
     /// <returns>A double-precision floating point number greater than or equal to 0.0, and less than 1.0.</returns> 
     public static double NextDouble() 
     { 
      lock (myLock) 
      { 
       return random.NextDouble(); 
      } 
     } 

     /// <summary> 
     /// Fills the elements of a specified array of bytes with random numbers. 
     /// </summary> 
     /// <param name="buffer">An array of bytes to contain random numbers.</param> 
     /// <exception cref="ArgumentNullException">buffer is a null reference (Nothing in Visual Basic).</exception> 
     public static void NextBytes(byte[] buffer) 
     { 
      lock (myLock) 
      { 
       random.NextBytes(buffer); 
      } 
     } 
    } 
} 
+0

我认为你的东西!页面引号“如果你使用相同的种子值两次,你会得到相同的随机数序列,随机使用当前时间作为种子。上面的代码创建了几个非常快速的连续实例,”当前时间“往往具有至少10ms的粒度,因此许多实例将共享相同的种子,从而创建相同的数字序列。“ 我相信代码正在执行,快速获得与其他面板相同的时间码“种子”,因此相同的图像。我需要一个更强大的随机方法。 – 2009-10-12 20:54:11

+0

@Ian - 看看Jon的MiscUtility类有一个“Robust”随机生成器。 (链接在帖子中)。我也编辑过这个帖子来包含他的代码。 – 2009-10-12 21:28:42

+0

@metro smurf,使用错误代码解决了问题。感谢大家的帮助。 – 2009-10-12 21:47:38

0

你确定你的页面不被缓存?在页面上查看源代码。

哦,应该有一些像urand或srand这样的函数来使随机更随机。

+0

视图源显示了4个面板具有相同和backgroundImage网址。刷新页面有时会用另一组4个图像替换所有4个图像,或者有时保持不变。 – 2009-10-12 20:48:45

0

我想知道是否在面板上有一定程度的缓存操作,导致它无法在生产模式下通过服务器端处理。

+0

服务器端代码必须在图像确实发生变化时工作,但在生产站点上,它会更改所有4个面板图像。在调试模式下运行可确保每个面板都有自己的随机图像...... – 2009-10-12 20:50:14

+0

确切地说 - 它几乎听起来像面板在第一个实例上运行了服务器端处理,然后使用控制的缓存版本进行迭代2 - 4.我想知道这是否是由于一些生产模式优化。 Metro Smurf对随机生成有一个很好的问题 - 你是否能够消除这个问题?如果是这样,我会进一步研究任何.net缓存,可能会在页面或应用程序级别 – 2009-10-12 20:56:57

+0

仍在调查随机数字代码 – 2009-10-12 21:06:09