Forum Moderators: open

Message Too Old, No Replies

HELP: random text

need help on java, please

         

eric_c

8:59 am on Nov 9, 2005 (gmt 0)

10+ Year Member



hi there, i'm new here and pretty new to java programming. i need a little help on programming a random text java program to show on a html website. the purpose of the program is to have the random quotes function. i found a code which randomize my quotes but it doesn't work everytime.

the code is:

<SCRIPT>
quote1="quote1"
.
.
.
quote30="quote30"

today2=new Date()
len=3
today=today2.getTime( )/10
rnd=today%len
document.writeln(eval("quote"+rnd))

-->
</SCRIPT>

it keeps showing the first two quotes or nothing (out of 30). i wonder if anyone can help me, please!
is it possible to set a range to the script? say randomly pick from quote10 to quote20 or i can expand it later and so on.

i appreciate if anyone can give me a little help, thanks!

Scally_Ally

10:46 am on Nov 9, 2005 (gmt 0)

10+ Year Member



try using javascripts random number function to return a number between 0 and 1 and then multiplying it by the number of options.

Something like.

<SCRIPT>
quote1="quote1"
.
.
.
quote30="quote30"

var myRandom = Math.floor(Math.random()*31)
document.write(eval("quote"+myRandom))

-->
</SCRIPT>

multiplying the random number by 31 gives a value between 0 and 30.

You can manipulate this to give values between 10 and 20..

Ally

Bernard Marx

11:03 am on Nov 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var quotes =
[
".A.",
".B.",
".C."
];

document.write( quotes[ Math.floor( Math.random()*quotes.length ) ] );

eric_c

11:47 am on Nov 9, 2005 (gmt 0)

10+ Year Member



thanks guys!
got it working!
it's a great forum here!
thanks again

adamnichols45

11:56 am on Nov 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also try to learn the difference between java and javascript