Click here to Skip to main content
15,896,329 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Can Anyone help me...

in my coding generating random number upto 3 digits i want from 4 digits.. what can i do for that.... and also it generate 2 times page loading & submit time..but i want only when page is load...ple help me..

coding:



C#
public void securitykey()
   {
       Random randobj = new Random();
       byte[] randArray = new byte[2000];
       randobj.NextBytes(randArray);
       for (int i = 0; i < randArray.Length; i++)
           Label4.Text = randArray[i].ToString();
   }
Posted
Updated 12-Dec-16 19:56pm
Comments
Sergey Alexandrovich Kryukov 22-Dec-11 1:03am    
Why do you think a security key of 4 digits could be enough? Even in hexadecimal digits, this is only a matter of trying no more than 65536 times... (Or gosh, our vulnerable, vulnerable checking accounts :-(
In security, people usually consider 1-2 Kilobytes keys as absolute minimum... How about that?
--SA

First of all, I really cannot understand why so many people do the same bad mistake all the time? The Random object should be created only once (per lifetime of the while application domain). I cannot understand what's the rationale behind that? Don't do it! At least move initialization of randobj out of the loop.

(I also cannot stop be amazed about using sub-string "obj" or "object" in the name of object, "app" or "application" in the name of application, "arr" or "array" in the name of array, etc. How can it even come to one's mind?!)

The object randArray makes no sense, whatsoever. The hard-coded immediate constant is just amazing. And the loop…

Let's say you need four decimal digits. All you need is:

C#
Random random = new Random();
int value = random.Next(10000); //will generate a number 0 to 9999


—SA
 
Share this answer
 
v2
Comments
eshitagupta 28-May-13 19:24pm    
Error 56 'random' is a 'variable' but is used like a 'method' how solve that>>??
Sergey Alexandrovich Kryukov 28-May-13 19:31pm    
Sorry, it was my typo. Should be of course "random.Next".
—SA
how to generate random number 3digits
 
Share this answer
 
Comments
Patrice T 13-Dec-16 4:02am    
Open your own question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900