Open
Random Metodu Kullanımı

Image Klasöründen rastgele resim gösterir


protected void btnImg_Click(object sender, EventArgs e)
{
    Random rnd = new Random();
    string[] fileImages = System.IO.Directory.GetFiles(MapPath(@"\images\"));
    string empLink1 = fileImages[rnd.Next(fileImages.Length)];
    int index = empLink1.IndexOf("images");
    string result = empLink1.Substring(index + 0);
    imgRandom.ImageUrl = "/" + result;
    lblResult.Text = result;
}

<asp:Image ID="imgRandom" runat="server" Width="45" />
<asp:Label ID="lblResult" runat="server" />
<asp:Button Text="ImajGetir" ID="btnImg" OnClick="btnImg_Click" runat="server" /> 
    
////////////////////////////////////////////////////////////////////////////////////////////////////////



Rastgele renk değiştir

protected void btnRenk_Click(object sender, EventArgs e)
{
    Random r = new Random(DateTime.Now.Millisecond);
    System.Drawing.Color[] colours = 
{ 
 
    System.Drawing.ColorTranslator.FromHtml("red"),   //orange
    System.Drawing.ColorTranslator.FromHtml("#38e188"),   //green
    System.Drawing.ColorTranslator.FromHtml("orange"),   //yellw
    System.Drawing.ColorTranslator.FromHtml("blue"),
};

    int i = r.Next(0, colours.Length - 1);
    System.Drawing.Color c = colours[i];

    lblRenk.ForeColor = c;
    Panel1.Style.Add("style", "background-color:" + c + " ;position:absolute; top:200px; width:100%; height:75px ");
    Panel1.Style.Add(HtmlTextWriterStyle.BackgroundColor, c.ToString()); 
}
    
<asp:Label Text="Rengi Değiştir" runat="server" ID="lblRenk"/>
<asp:Panel ID="Panel1" runat="server" CssClass="pnltop">dsf sdf dsf df   </asp:Panel>
<asp:Button Text="RenkKoy" ID="btnRenk" OnClick="btnRenk_Click" runat="server" />
    
////////////////////////////////////////////////////////////////////////////////////////////////////////



Rastgele 10-a kadar sayı üret

public static int rastgeleSayiUret()
{
    Random rnd = new Random();
    return rnd.Next(10);
}
protected void btnSayi_Click(object sender, EventArgs e)
{
    ltrlSayi.Text = rastgeleSayiUret().ToString();
}

    
<asp:Literal ID="ltrlSayi" runat="server" />
<asp:Button Text="SayiUret" ID="btnSayi" OnClick="btnSayi_Click" runat="server" />

    
////////////////////////////////////////////////////////////////////////////////////////////////////////



6 adet harfler kümesinden rastgele harf üret

protected void btnHarf_Click(object sender, EventArgs e)
{
    Random rastgele = new Random();
    string harfler = "qwertyuıopasdfghjklzxcvbnm";
    string uret = "";
    for (int i = 0; i < 6; i++)
    {
        uret += harfler[rastgele.Next(harfler.Length)];
    }
    lblHarf.Text = uret;
}

    
<asp:Label ID="lblHarf" runat="server" />
<asp:Button Text="HarfUret" ID="btnHarf" OnClick="btnHarf_Click" runat="server" />

    
02.02.2012