ASP.NET (Web) + C#算法 | 生成隨機數字序列(隨機數字+每個數字取隨機不重復的位置和顏色)

關于今天的一個關于ASP的課后作業,是要求在ASP上實現隨機生成數字序列:

具體要求:

  1. 隨機位置:每個數字的位置相對隨機;
  2. 隨機顏色:每個數字的顏色隨機且不重復;
  3. 隨機數字:從0到9隨機取出四個數;



正文


首先放上核心算法,這里我覺得在common.cs中編寫比較妥當:

    public static int[] GetRandom(int minValue, int maxValue, int count)
    {
        int[] intList = new int[maxValue];//創建一個以  最大值大小  為長度的數組
        for (int i = 0; i < maxValue; i++)//數組的內容:最小值+(從 0 到 最大值減一 ),及intList為一個特殊規律的不重復的遞增數組
        {
            intList[i] = i + minValue;
        }
        int[] intRet = new int[count];//創建以  要取的數的個數  為長度的數組
        int n = maxValue;
        Random rand = new Random();
        for (int i = 0; i < count; i++)
        {
            int index = rand.Next(0, n);//隨機取一個0到n之間的數
            intRet[i] = intList[index];
            intList[index] = intList[--n];
        }

        return intRet;
    }

//n是一個遞減變化的數
//intList的一個運行模擬序列:
//0 1 2 3 4 n = listlength = 5,取到1
//0 4 2 3 | 4 n = listlength = 4,取到4
//0 3 2 | 3 4 n = listlength = 3
//...
//不斷用最后面的值來覆蓋選中到的值,再把最后面的值去掉(通過n--實現,抽象意義上“截短”提供數字的intList),由此實現不重復序列

詳細解析見以上的代碼截圖。



接著是.aspx.cs文件(下圖為部分剪影,后方附上完整代碼):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;
using System.Drawing;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Random rd = new Random();
        int num = rd.Next(1000,10000);
        string textString = num.ToString();

        int widthx = 800;
        int heightx = 600;
    
        Bitmap btmap = new Bitmap(widthx, heightx);
        Graphics gg = Graphics.FromImage(btmap);
        SolidBrush sb = new SolidBrush(Color.White);
        gg.FillRectangle(sb, new Rectangle(0, 0, widthx, heightx));
        
        Font ft = new Font("楷體",18,FontStyle.Strikeout);
        SolidBrush sbft = new SolidBrush(Color.Black);
        Color[] cr = {Color.Red,Color.Black,Color.Blue,Color.Yellow,Color.Gray,Color.Orange};
        //gg.DrawString(textString.Substring(0,textString.Length/2), ft , sbft, new PointF(0,0));
        //gg.DrawString(textString.Substring(5,5), ft, sbft1, new PointF(0, 300));

        int[] rdlist = common.GetRandom(0,cr.Length,textString.Length);//產生一個隨機的不重復的int列表

        int leftmargin = 0;
        for (int i=0; i < textString.Length; i++)
        {
            //使用時,順序對這個int列表取值即可
            gg.DrawString(textString.Substring(i,1),ft,new SolidBrush(cr[rdlist[i]]),leftmargin,leftmargin+100+rd.Next(-15,15));
            leftmargin = leftmargin + 18;
        }
        MemoryStream ms = new MemoryStream();
        btmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ClearContent();
        Response.ContentType = "image/gif";
        Response.BinaryWrite(ms.ToArray());
        
    }
}

至此便實現了要求了,下面放上效果圖:









算法參考文章

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。