C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Create random number with Random class


Posted Date: 21 Aug 2008    Resource Type: Code Snippets    Category: General
Author: JessicaMember Level: Gold    
Rating: Points: 10



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.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Random  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Picture blurry zooming using JAVA
Previous Resource: Setting Authentication Cookies
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use