Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
(Program in Javascript)

Write a 20-character random generator that uses the least resource (file size, memory, CPU). The valid characters
allowed in the key string are a-z, A-Z and 0-9.
e.g. 83FEdmhjKla4WiYd6uem
Posted

if you can create GUIDs in javascript, just do that, and remove the hypens and curly braces if necessary. I found this with google:

JavaScript
function createUUID() 
{ 
    // http://www.ietf.org/rfc/rfc4122.txt     
    var s = []; 
    var hexDigits = "0123456789abcdef"; 
    for (var i = 0; i < 36; i++) 
    {    
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);     
    } 
    s[14] = "4";  
    // bits 12-15 of the time_hi_and_version field to 0010     
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  
    // bits 6-7 of the clock_seq_hi_and_reserved to 01     
    s[8] = s[13] = s[18] = s[23] = "-";      
    var uuid = s.join("");     
    return uuid; 
} 


You can change the contents of hexDigits to include more alphabetic characters, adjust the for loop to only generate 20 characters, and remove the code at the bottom of the function that adds the hyphens.
 
Share this answer
 
v3
 
Share this answer
 
Java
 <script type="text/javascript">
        function generateRandomString() {
            chars = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "g", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
            var x = "";
            for (i = 0; i < 20; i++) {
                x += chars[randomnumber = Math.floor(Math.random() * 36)];
            }
            return x;
        }
//test it with this line
        //window.alert(generateRandomString());</script>
//to add capital letters just add them to the array and put 62 instead of 36
 
Share this answer
 
v2
C#
function GetRandomString() {
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

   for (var i = 0; i < 20; i++) {

    text += possible.charAt(Math.floor(Math.random() * possible.length));
   
   }

  return text;
}
 
Share this answer
 
Comments
CHill60 12-Feb-15 5:26am    
Question was answered over 3 years ago!
Deepu S Nair 12-Feb-15 6:03am    
Ans he is reposting it
CHill60 12-Feb-15 6:24am    
And there is very little difference between this and solution 4 :)
Deepu S Nair 12-Feb-15 6:27am    
smart :)
C#
function GetRandomString() {
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

   for (var i = 0; i < 20; i++) {

    text += possible.charAt(Math.floor(Math.random() * possible.length));

   }

  return text;
}
 
Share this answer
 
Comments
Deepu S Nair 10-Feb-15 1:12am    
Question is nearly 4 years old and also you are reposting your answer
Santosh K. Tripathi 10-Feb-15 5:49am    
Is this answer wrong?
or 'ttds' has accepted any solution and hence question is closed?
or a person can't answer a question if question is old?

I was searching for some this and found this question. I answered it because some time earlier i was facing same issue.
Deepu S Nair 12-Feb-15 6:03am    
but why are you reposting your answer?
CHill60 12-Feb-15 6:26am    
No the answer is not wrong. However there is little difference between what you have posted and what Sara Dark posted in Solution 4 years ago.
Santosh K. Tripathi 12-Feb-15 23:27pm    
thanks for response. My solution is showing here two time, really i don't know how it happen.

we can discourse little bit more. i want to put my few points.

1. in my answer i did use 'new Array' keyword because

http://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar

http://stackoverflow.com/questions/7375120/why-is-arr-faster-than-arr-new-array

2. I used '*possible.length' instead of '*36 (no need to calculate array length manually, easy for maintenance if you add/remove few characters in array).
You can use below code for the same.

<html>
<body>

Click the button to display a random number.

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {

var Output = "";
var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
   for (var i = 0; i < 20; i++) {
 
    Output += characters.charAt(Math.floor(Math.random() * characters.length));
   
   }
 
    document.getElementById("demo").innerHTML = Output;
}
</script>

</body>
</html>
 
Share this answer
 
Comments
CHill60 12-Feb-15 5:25am    
Question was answered over 3 years ago!

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