Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Random Number But Only Once

    This is quite easy yet hard.

    I'm trying to apply something in my forums, where a member could post something let's say:

    "Hi, I am number [random]1, 6[/random]"

    And what would happen is that, a number from 1 to 6 would be the content, for example, it would be:

    "Hi, I am number 3"

    But if you refresh the page, it would still be the same number. Like it won't be randomized again.

    So how do I make a javascript/html code that would make a one time number randomizer.
    Last edited by Undisclosed; 08-31-2011 at 04:57 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If you refresh the page everything on that page is lost. You must use a cookie to store information to be carried over to another session. Remember that people can (and do) delete cookies in their browser.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    120
    Thanks
    1
    Thanked 15 Times in 15 Posts
    Seems a bit odd, but ok.

    You do not want a call to random() to be included in the inline message. You want the output of random() to be posted in the message. To get that, I would (assuming that you have a toolbar in the typing area) put an interface to random() that prompts for parameters then posts it's result in the message.

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    22
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Yeah, every time I refresh the page, the numbers changes.
    Is there a way to convert them to plain text, right after I submit.

    Right now here's my code:
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    num= new Array(6);
    for (i=0; i<1; i++) {
    num[i]=Math.floor(6*Math.random()+1);
    
    document.write(num[i]);
    }
    
    </SCRIPT>
    I would do the cookie thing, but I don't know how to do that. >.<
    Last edited by Undisclosed; 08-31-2011 at 06:44 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You cannot do anything after a submit! That wipes out and reloads the page!

    Cookies have been covered very many times in this forum. Try using the Search feature.

    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    To convert numbers to their written text form:-

    Code:
    <script type = "text/javascript">
    
    var nums = ["One", "Two", "Three", "Four", "Five", "Six"];
    var len = nums.length;
    var x = Math.floor(len*Math.random());
    alert (nums[x]);  // a random number from One to Six
    
    </script>
    Last edited by Philip M; 08-31-2011 at 07:31 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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