Click to See Complete Forum and Search --> : math random word arrays
Melons
11-29-2003, 07:06 AM
Hi,
can anyone help me please?
What I want to do is to be able to select a random array of words/phrases from several individual arrays and then depending on the word /phrase selected to be able to force to another array for the next line. In my html file I have the following :
_______________________
var txt = "<p align='center'><font face='Arial' size=5 color=#400040>"
var hold1=1
var hold2=1
for (i = 0; i <= 0; i++) // changes number of verses
{hold1=Math.round(Math.random() )
hold2=Math.round(Math.random() )
txt = a1[Math.round(Math.random() * 164)] + " "
+ b1[Math.round(Math.random() * 107)] + " "
+ c1[Math.round(Math.random() * 83)] + " "
_____________________
and in my js file I have the following :
var a1 = new Array(164)
var b1 = new Array(107)
var c1 = new Array(83)
and then all the words for a1, b1 and c1 listed.
______________________
This works fine for the moment as a basic phrase, word, poem generator.
What I want to do is to have a random word taken from a1, b1 and c1 arrays combined and then depending on the word chosen to be able to select the next word from a different array.
For instance, the first line of text I would want to be randomly chosen from a1 and b1 and c1.
The second line of text would depend on which word was chosen and from which array it came from, so, if the first line was randomly chosen from a1, b1 and c1 and the actual word/phrase chosen came from b1 then I would want the next line to choose from an array b2. If, however, i the first line was randomly chosen from a1, b1 and c1 and the actual word/phrase chosen came from a1 then I would want the next line to choose from an array a2 etc. In this way I can place similar words and phrases within an array and then, depending on what one is chosen force the system to randomly select a related word or phrase from another array.
Does anyone know how I can do this please?? Did I explain it ok or have I just confused everyone?
Best regards,
Mel ...fingers crossed someone can help me as I am a bit naff at this kind of thing...........(examples of code written for me might be nice lol *winking)
Melons
11-29-2003, 08:34 AM
Writing a poetry generator is not easy, but I have kind of managed to do one here, Poem Generator (http://www.melswebs.com/generators/nature.htm) with js file (http://www.melswebs.com/generators/nature.js) .
The problem is that words/phrases for winter (as an example) will be thrown together with words/phrases pertaining to summer etc. (with me so far?)
So, I want each season in a separate array in the js file (for instance Winter=a1, Spring=b1, Autumn=c1, Summer=d1), where each array contains phrases for that season.
Then I want the first line of the poem to randomly pick from a1, b1, c1 and d1 combined.
Then, if the system has selected a random phrase from, for instance c1 (Autumn) then the next line in the poem will be forced to select a phrase from another array such as c2 which will contain more Autumnal related phrases or words.
This will mean that the poems generated will always maintain a theme throughout the poem, without me having to create multiple separate js files and htm files for each season.
Hope this is clearer........can it be done?
Sorry I typed so much, kinda like verbal diarrhoea I think!
All the best,
Mel
batfink
12-02-2003, 07:37 AM
After your first line has been chosen and then 2nd line, what do you want to happen to the rest of the poem? Repeat this so two lines come from the same section????
Melons
12-03-2003, 06:28 PM
After your first line has been chosen and then 2nd line, what do you want to happen to the rest of the poem? Repeat this so two lines come from the same section????
Well, hopefully, if someone can help me figure out the first 2 lines problem i will be able to sort the rest of the coding myself, by repeating with a third line from arrays a3, b3, c3, or d3 which refer to the seasons from line1, as I quote:
(for instance Winter=a1, Spring=b1, Autumn=c1, Summer=d1), where each array contains phrases for that season.
then a fourth line referring to season in line 1.
This would then be repeated for another 4 lines, random line 1 from 4 arrays, a1, b1, c1 or d1 and then if random first line was selected from c1 to pick line 2, 3 and 4 from c2, c3 and c4 which pertain to that season..
is that clearer?
anyway, thanks for replying :rolleyes:
Mel
batfink
12-03-2003, 08:44 PM
You need to create a new array with each section in.
var bat = new Array;
bat[0]=a1;
bat[1]=b1;
and so on.
Then use your random method on this to feed the code you already have.
feed = bat[Math.floor(Math.random()*max sections)]
so feed will be a1 or b1 or c1 etc.
Its 0230 here so if that don't make sense I'll make it clearer during daylight hours....:o :o
Melons
12-04-2003, 05:56 AM
Hi there batfink :)
I can see you burning the midnight candles from here!
errrr...............can you explain it a little easier for me as I am a bit crap at java :( ....... kinda like which bits of code go where (js or htm file), and in what order. I spent an hour or so on it again this morning and still cant figure it
:rolleyes:
<-----here's hoping you can keep your eyes open long enough to help me out again tonight (so to speak :D )
Cheers darlin'
Mel
batfink
12-04-2003, 08:07 AM
:p LOL
Hi Mel,
Hey I'm rubbish at Java too...shall we code in JavaScript??
:rolleyes:
Sorry had to tease.
In your js file an example line is:
a1[163]="An opulent"
You are after another choice from a1 right?
First create new array with all those sections of the js in it.
var bat = new Array("a1","b1", .........."z3");
Thats how you choose a section and you know how to chose a word from a section as you have this code already, so use that random code to pick a random section.
Hey if you wrote that poem code yourself you're not that bad.
Melons
12-04-2003, 06:58 PM
Hey there Batfink (I was gonna get familiar and call you batty :cool: )
I've kind of made a start and placed the current .js and .htm files online for you to have a look at (if you feel that way inclined of course ;) )
located here nature.html (http://www.melswebs.com/generators/test/nature.htm)
.js file (http://www.melswebs.com/generators/test/nature.js)
I placed a small amount of phrases in the .js and htm files to make it easier to see what I am trying to do (I think I got lost somewhere!)
I managed to get a random 'bat' to come up, but it is not reading the file as I expected, so, instead of it coming up with, for instance, a winter phrase (Winters morning) it is coming up with c1. From the coding
var bat = new Array;
bat[0]=a1;
bat[1]=b1;
bat[2]=c1;
bat[3]=d1;
so I got lost at first base somewhere! Heeeeellllllpppp!!!!!
when it does this I will be asking the next round of questions (so beware lol)
and, yep I did do the coding for the other poem generator, but that was pretty straightforward and there seem to be loads of tutorials with basic coding and hints and tips, so I made a chimera of code snippets and manipulated and tweaked it until it did what I wanted :rolleyes:
Anyway, this one has me flummoxed
:confused:
Now don't stay up until the wee hours trying to solve it for me, I can wait (a little while at least lol......yawning and off to my own bed now.....ZZZZzzzzzz)
you take care and cheers for now babes,
Mel x
batfink
12-09-2003, 08:39 AM
Hi Melons,
At last you say!
The code works beautifully. Attached are the two files.
(note the path to nature.js - I have just made it look in the same place as the html file)
Quick tip. You can declare and fill an array in two ways.
var bat = new Array("a","b","c","d");
OR
var bat = new Array();
bat[0]="a";
bat[1]="b";
bat[2]="c";
bat[3]="d";
Just use what is best in the situation. Like in the one above why write loads more code and chose the second example when the first is best.
Anyway check the code out and I'll prepare myself for your onslaught of questions.:p
Melons
12-10-2003, 06:16 PM
Hey Batty!!!
You are a darling and a genius :)
I was out doing the xmas shopping thing last night and tonight, so only just had a chance to download and have a quick peep at your code.......and it works a real treat :D
I will have a go at populating the .js file more tomorrow night and checking out the code more fully....so you might get some more questions from me
:rolleyes:
What more can I say...I'm gobsmacked that you took the time to figure it out for me (hopefully you weren't up all night doing it though....errr......writing the code I mean ;) ).
Watch this space....
Cheers babes....
Mel
(blowing you a well-deserved, whopping big kiss xoxoxox)
batfink
12-11-2003, 02:46 AM
You crack me up.:p