3

I want to generate a string of random letters say 10 letters from a-z one after the other i.e. the next letter should be displayed after the previous letter after a certain delay, later, I want to calculate the number of times each letter has been generated, unlike what I have done previously, i.e. I have taken a predefined array of letters and generated them accordingly.

  • possible duplicate of [Generate a string of 5 random characters in Javascript](http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript) – Phrogz Apr 20 '13 at 17:49
  • When should it count number of occurrences? Where should those numbers be printed. Clarify your question. – dfsq Apr 20 '13 at 17:55
  • As soon as the string is generated , the no of occurrences may be counted, and they could be displayed in a text box or a text area... –  Apr 20 '13 at 18:02

3 Answers3

5

Shorter way to generate such a string using String.fromCharCode:

for (var i = 0, letter; i < 10; i++) {
    setTimeout(function() {
        letter = String.fromCharCode(97 + Math.floor(Math.random() * 26));
        out.appendChild(document.createTextNode(letter)); // append somewhere
    }, 2000 * i);
}

And complete demo covering all the problems in this question: http://jsfiddle.net/p8Pjq/

dfsq
  • 191,768
  • 25
  • 236
  • 258
3

Use the setInterval method to run code at an interval. Set up an array for counting each character from the start, then you can count them when you create them instead of afterwards:

var text = '';
var chars = 'abcdefghijklmnopqrstuvwxyz';
var cnt = new Array(chars.length);
for (var i = 0; i < cnt.length; i++) cnt[i] = 0;

var handle = window.setInterval(function(){
    var ch = Math.floor(Math.random() * chars.length);
    cnt[ch]++;
    text += chars.charAt(ch);
    $('#display').text(text);
    if (text.length == 20) {
      window.clearInterval(handle);
      // now all characrers are created and counted
    }
}, 2000);

Demo: http://jsfiddle.net/R8rDH/

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I was working on answering, but this seems like a better way than I was working on. Nice job. – dewyze Apr 20 '13 at 18:10
1

I am stealing this answer, but look here: Generate random string/characters in JavaScript

function makeid()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
Community
  • 1
  • 1
dewyze
  • 979
  • 1
  • 7
  • 21
  • For Math.random() == 1, we would get a "" string. It should be possible.charAt(Math.round(Math.random() * (possible.length - 1))); – Parthik Gosar Apr 20 '13 at 17:49
  • 1
    @Parthik JavaScript's `Math.random` returns values in the range `[0,1)`, i.e. never exactly 1 http://stackoverflow.com/questions/5580579/javascript-math-random – Phrogz Apr 20 '13 at 17:50
  • 1
    @ParthikGosar: The `Math.random` method never returns 1. The random number is 0 <= n < 1. Using `round` for the random number means that the first and last character occurs half as often as they should. – Guffa Apr 20 '13 at 17:51
  • Math.random never returns 1. EDIT: I was too slow, what they said is right. – dewyze Apr 20 '13 at 17:51
  • thank you, but thats not exactly what I m looking for, consider the random string to be generated is AZXNYRAO, then Z should be displayed 2 secs after A is displayed... and then it should be calulated how many times each letter has occurred.. say A=3, X=1 so on... –  Apr 20 '13 at 17:52
  • @Phrogz, Guffa and JDewzy. Thanks. I din't know that ! – Parthik Gosar Apr 20 '13 at 17:55
  • so... can any one please help me..? –  Apr 20 '13 at 18:03