Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C ASP.NET
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:
 

 
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 21-Dec-11 18:24pm
Comments
SAKryukov at 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

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
Random random = new Random();
int value = random.Next(10000); //will generate a number 0 to 9999
 
—SA
  Permalink  
v2
Comments
eshitagupta at 28-May-13 19:24pm
   
Error 56 'random' is a 'variable' but is used like a 'method' how solve that>>??
Sergey Alexandrovich Kryukov at 28-May-13 19:31pm
   
Sorry, it was my typo. Should be of course "random.Next".
—SA

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

  Print Answers RSS
0 OriginalGriff 489
1 ChintanShukla 295
2 RyanDev 250
3 Richard Deeming 250
4 Sergey Alexandrovich Kryukov 218
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,601
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,923


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 28 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100