Hi All,
The code below describe ,how to generate random number with the use of the "Random" class. I have follow this steps to achieve this.
1. Take a string variable dictionary and save alphabet and number. 2.Take a object of Random class. 3. Input a length of the code to be generated. 4. For that amount of time use for loop and multiply it with system time ticks. 5. ObjRand.Next will retrun the non-negative number less then the specified maximum number(here it is 20). One can define the minimum also. 6. Return the random number.
private string CreateRandomNumber(int CodeLength) { string dictionary = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; string[] dictArray = dictionary.Split(','); string randomCode = ""; int temp = -1;
Random ObjRand = new Random(); for (int i = 0; i < CodeLength; i++) { if (temp != -1) { ObjRand = new Random(i * temp * ((int)DateTime.Now.Ticks)); } int t = ObjRand.Next(20); if (temp == t) { return CreateRandomNumber(CodeLength); } temp = t; randomCode += dictArray[t]; } return randomCode; }
Hope it helps. Feedback are welcomed. :)
Warm Regards.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|