Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Apr 2004

    Unhappy javascript random NOT random

    function setFortune() {
    var currFookie = "fookie";
    var myFortune = new Array(
    "You are talented in three ways",
    "Idleness is a holiday for fools",
    ... there are 101 strings in his array ...
    "Your cell phone batteries run without you"
    var randomFookie = Math.floor((Math.random() * myFortune.length));
    document.getElementById(currFookie).innerHTML = myFortune[randomFookie];

    This code produces a consistent random pattern, therefore not random. Every time I launch the application on my Nokia E71 device and on the several devices available via fabulous Nokia testing site at http://apu.ndhub.net/devices, I get a consistent results:
    // random order 20090428 17:35 lines 44,8,31,28,45
    // random order 20090429 14:37 lines 44,8,31,28,45
    // random order 20090429 15:02 lines 44,8,31,28,45

    Any thoughts on how to solve this. I've been banging my head on this for few days and cross checking multiple sources and I have seen no mention of this elsewhere.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Oulu, Finland

    Re: javascript random NOT random

    random() is not random but pseudorandom. It will always produce the same sequence given the same seed number. One way to make it appear more random is to provide it a changing seed number, e.g.
    now = new Date();
    seed = now.getSeconds();
    var randomFookie = Math.floor((Math.random(seed) * myFortune.length));

  3. #3
    Registered User
    Join Date
    Apr 2004

    Smile Re: javascript random NOT random

    Thank you. This just what I wanted.


Similar Threads

  1. Replies: 3
    Last Post: 2008-11-11, 10:18
  2. Random RingTone within Contact List for 3rd
    By cckfranky in forum Symbian
    Replies: 5
    Last Post: 2007-12-29, 18:33
  3. Problems with random nubers ...
    By IngeSchmidt in forum Symbian Tools & SDKs
    Replies: 0
    Last Post: 2004-07-27, 16:39
  4. problem with Random()
    By doomer999 in forum Mobile Java General
    Replies: 2
    Last Post: 2003-05-05, 15:16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts