How to replace random number in asp.net/c with pictures?
I am trying to replace random number with pictures. for example If random number is 1 show picture black.jpg:
Cache[diceKey] = r.Next(1, 5); // random (1-4)
if (r.Next(1, 2) =开发者_运维知识库= 1 )
image.BackImageUrl = "Images/black.png";
is there any solutions that I can show my picture if random number is 1?
Store your image urls in an array. Generate a random index to access the image:
string[] imageUrls = new [] { "foo.png", "bar.png", "foobar.png" };
// ...
Random r = new Random();
image.BackImageUrl = imageUrls[r.Next(imageUrls.Length)];
Update (still not getting the issue, though)
Only set a certain image if the generated random number is 1:
int i = r.Next(1, 5); // random (1-4)
Cache[diceKey] = i;
if (i == 1)
{
image.BackImageUrl = "Images/black.png";
}
As I already said in my comment r.Next(1, 2)
will always return 1, so you will always show that black image...
I think you should change your code to this:
var randomValue = r.Next(1, 5); // random (1-4)
Cache[diceKey] = randomValue;
if (randomValue == 1)
image.BackImageUrl = "Images/black.png";
else
image.BackImageUrl = "";
精彩评论