473,378 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problem with Random Quote Generator

I'm hoping this is very simple. I am currently using a random quote
generator on our intranet.

I have 17 quotes, when I add in an 18th and change the makeArray
number the code seems not to work.

This works:

<script LANGUAGE="JavaScript">
function makeArray(len) {
for (var i = 0; i < len; i++) this[i] = null;
this.length = len;
}
quotes = new makeArray(17); //This Number is a variable relating to
the Number of Quotes//
quotes[0] = "\"Imagination is more important than knowledge \" Albert
Einstein";
quotes[1] = "\"One should always play fairly when one has the winning
cards. \" Oscar Wilde"; ...............
.............
quotes[16] = "\"Well, if I called the wrong number, why did you answer
the phone? \" James Thurber";

function rand(n) {
seed = (0x015a4e35 * seed) % 0x7fffffff;
return (seed >> 16) % n;
}
var now = new Date()
var seed = now.getTime() % 0xffffffff
</script>

<script LANGUAGE="JavaScript">
var quoterand = quotes[rand(quotes.length)];
if (quoterand.length < 40) {
document.write(quoterand+"")
} else { document.write(quoterand)
}

</script>
All I od is add another quote, increase the makeArray and it goes
wrong. Any ideas?
TIA
Luke
Jul 23 '05 #1
4 2466
lharby wrote:
I'm hoping this is very simple. I am currently using a random quote
generator on our intranet.

I have 17 quotes, when I add in an 18th and change the makeArray
number the code seems not to work.

This works:

<script LANGUAGE="JavaScript">
function makeArray(len) {
for (var i = 0; i < len; i++) this[i] = null;
this.length = len;
}
quotes = new makeArray(17); //This Number is a variable relating to
the Number of Quotes//
quotes[0] = "\"Imagination is more important than knowledge \" Albert
Einstein";
quotes[1] = "\"One should always play fairly when one has the winning
cards. \" Oscar Wilde"; ...............
............
quotes[16] = "\"Well, if I called the wrong number, why did you answer
the phone? \" James Thurber";

function rand(n) {
seed = (0x015a4e35 * seed) % 0x7fffffff;
return (seed >> 16) % n;
}
var now = new Date()
var seed = now.getTime() % 0xffffffff
</script>

<script LANGUAGE="JavaScript">
var quoterand = quotes[rand(quotes.length)];
if (quoterand.length < 40) {
document.write(quoterand+"")
} else { document.write(quoterand)
}

</script>
All I od is add another quote, increase the makeArray and it goes
wrong. Any ideas?
TIA
Luke

Hi Luke,

For what you are trying to do, the code used is complete overkill.
Have a look at this.
<html>
<head>
<script type="text/javascript">
var quotes = ["MyFirst Quote" , "Q2" , "Q3", "Q4", "Q5", "Q6", "Q7", "Q8",
"Q9", "Q10", "Q11", "Q12", "Q13", "Q14", "Q15"];

function getRandomQuote(){
numberOfQuotes = quotes.length;
randNum = Math.floor(Math.random()*numberOfQuotes);
return quotes[randNum];
}
</script>

</head>
<body>
Hai, the random quote is:
<br>
<script type="text/javascript">
document.write(getRandomQuote());
</script>

</body>
</html>
Jul 23 '05 #2
Erwin Moller wrote:
[...]
function getRandomQuote(){
numberOfQuotes = quotes.length;
randNum = Math.floor(Math.random()*numberOfQuotes);
return quotes[randNum];
}


Very concise, but you could take that a little further:

function getRandomQuote(){
return quotes[Math.floor(Math.random()*quotes.length)];
}
--
Fred
Jul 23 '05 #3
Fred Oz wrote:
Erwin Moller wrote:
[...]
function getRandomQuote(){
numberOfQuotes = quotes.length;
randNum = Math.floor(Math.random()*numberOfQuotes);
return quotes[randNum];
}


Very concise, but you could take that a little further:

function getRandomQuote(){
return quotes[Math.floor(Math.random()*quotes.length)];
}


Well Fred, :-)

You are right of course, but I was trying to keep the steps readable for the
Original Poster.
For educative purposes. :-)

Regards,
Erwin Moller
Jul 23 '05 #4
Thanks one and all (at Developersdex)

Worked like a charm, and much cleaner code.
:]
Luke
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
3
by: guy | last post by:
does anyone have good random number generator? the .NET one does not generate a wide enough range of values as i have to populate a psuedo array that has approx 500 million entries, using the .NET...
3
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
3
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a method which produces a random DNA sequence. I would like to write a unit test for it, but can't quite figure out what makes sense. It is easy to test that a DNA sequence is produced....
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.