About today's homework about ASP, it is required to realize random number sequence generation on ASP:
Specific requirements:
- Random position: the position of each number is relatively random;
- Random color: the color of each number is random and not repeated;
- Random number: take four numbers randomly from 0 to 9;
text
First, put on the core algorithm. I think it's appropriate to write it in common.cs:

public static int[] GetRandom(int minValue, int maxValue, int count) { int[] intList = new int[maxValue];//Create an array of maximum size for (int i = 0; i < maxValue; i++)//Contents of the array: Min + (minus one from 0 to max), and intList is a non repeated increasing array with special rules { intList[i] = i + minValue; } int[] intRet = new int[count];//Create an array with the length of the number to be fetched int n = maxValue; Random rand = new Random(); for (int i = 0; i < count; i++) { int index = rand.Next(0, n);//Take a random number between 0 and n intRet[i] = intList[index]; intList[index] = intList[--n]; } return intRet; }
//n is a decreasing number
//A run simulation sequence of intList:
//0 1 2 3 4 N = listlingth = 5, take 1
//0 4 2 3 | 4 N = listlingth = 4, take 4
//0 3 2 | 3 4 n = listlength = 3
//...
//Continuously use the last value to cover the selected value, and then remove the last value (through n-implementation, abstractly "truncate" to provide the intList of numbers), so as to realize non repetitive sequence
See the code screenshot above for detailed analysis.
Next is the. aspx.cs file (the figure below is a partial silhouette, followed by the full code):

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("Regular script",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);//Generate a random list of distinct int s int leftmargin = 0; for (int i=0; i < textString.Length; i++) { //When using, you can take values from the int list in order 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()); } }
So far, the requirements have been realized, and the following renderings are placed:


