Advertisement
Guest User

random character generator

a guest
Mar 29th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.11 KB | Source Code | 0 0
  1. /* random character generator */
  2. /* Mobile users can test this script by copying it and pasting it after "javascript:" in the web browser's URL address bar. */
  3.  
  4. /* array which lists the characters */
  5. var characters = ["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",  "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",  0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  6. var select_char = 0; /* Initializing variable to defeat JSHint warning; no functional difference. */
  7.  
  8. function random_char() {
  9. /* randomly selects a position inside the array */
  10. select_char = Math.floor(Math.random() * characters.length);
  11.  
  12. /* Prevents an error in the unlikely event that Math.random() results into exactly 1. It is unsure if it ever happens, but for being on the safe side. */
  13. if (select_char == characters.length) select_char-=1;
  14.  
  15. /* returns character from selected position */
  16. return characters[select_char];
  17. }
  18.  
  19. /* alert character for demonstration purposes */
  20. alert(random_char());
  21.  
Tags: array
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement