关于随机函数的问题

时间:2009-11-09 09:33:03

标签: c# random

我是C#编程的新手。我的问题是这个;

我怎么能在生成随机数后(我到目前为止)得到这些数字来显示图像框中的随机图像?

我有4个带有backgroundimage的图像框。 (但我希望他们根据我生成的数字进行更改。)

我该怎么做?

有人有个主意吗?

提前thx !!

2 个答案:

答案 0 :(得分:7)

保留图像列表,并将图像设置为列表中的随机元素:

public class SomeHostingForm
{
    private readonly List<Image> images; // Populate elsewhere...
    private readonly Random random = new Random();

    private void SwitchImage(ImageBox box)
    {
        box.Image = images[random.Next(images.Count)];
    }
}

(我不确定你说的是哪种类型,所以这有些伪代码。)

答案 1 :(得分:3)

将图像框放入数组中,然后使用随机数作为数组的索引来获取随机图像框。