View Full Version : Random Number But Only Once

08-31-2011, 05:51 PM
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 1, 6"

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.

Philip M
08-31-2011, 05:54 PM
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.

08-31-2011, 06:07 PM
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.

08-31-2011, 07:40 PM
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:


num= new Array(6);
for (i=0; i<1; i++) {



I would do the cookie thing, but I don't know how to do that. >.<

Philip M
08-31-2011, 08:28 PM
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:-

<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