473,809 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript">
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] = "\"Imaginat ion 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="JavaS cript">
var quoterand = quotes[rand(quotes.len gth)];
if (quoterand.leng th < 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 2504
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="JavaS cript">
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] = "\"Imaginat ion 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="JavaS cript">
var quoterand = quotes[rand(quotes.len gth)];
if (quoterand.leng th < 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()*numbe rOfQuotes);
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()*numbe rOfQuotes);
return quotes[randNum];
}


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

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


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

function getRandomQuote( ){
return quotes[Math.floor(Math .random()*quote s.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
3251
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 library, which is based on GTK. Depending on which libraries I link in, I can have either a GTK1 or GTK2 user interface. I'm finding that the simulation behaves differently depending on whether I link with GTK1 or GTK2.
11
1845
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 System.Threading; using System.Web.Services;
5
3355
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 double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
3
1422
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 generator the same sequence eventually repeats. oh it has to fast too :)
3
4690
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 Random((int)_seedTimer.ElapsedTicks); _random = new Random(randomSeed.Next(0,99999999)); return random.Next(min, max);
21
13517
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
2739
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. What I can't figure is randomness. By it's very nature random is random. I have written a simple test that generates 500 sequences and checks that none of them are the same. That works, but seems very weak besides, if the sequences are truly...
16
539
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 given a seed. - Relatively uniform, does not have to be perfect. The application is not a security or statistics application, the quality of numbers is not a priority although a fairly uniform
26
7924
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()); int r2 = rand(); bool f = r1 == r2;
16
2088
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
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.