# Random txt out of txt file

**URL:** <https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876>\
**Category:** Uncategorized\
**Created:** [May 2, 2003, 10:20am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876 "2003-05-02T10:20:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![leenx](https://avatars.discourse-cdn.com/v4/letter/l/ed655f/32.png) [@leenx](https://forum.kirupa.com/u/leenx)\
**Post date:** [May 2, 2003, 10:20am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/1 "2003-05-02T10:20:11Z")

</div>

Hi,

I want to get a random tekst out of a txt file,  
(random txt1, txt2, txt3)

txt:

&txt1=bla  
&txt2=bla  
&txt3=bla

Code i have so far  
loadText = new loadVars();  
loadText.load(“data.txt”);  
loadText.html = yes;  
//creating the loadVarsText function  
loadText.onLoad = function() {  
txt.html = true;  
txt.htmlText = this.txt1;  
};

If anyone knows a good turorial for this, couldn’t find anyting

Thanx

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 2, 2003, 10:28am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/2 "2003-05-02T10:28:53Z")

</div>

well if the rest of it is working there add this line:

randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]

just list all the “txt” variables between the [] brackets!  
This creates an array of the txt variables

and replace this line:

txt.htmlText = this.txt1;

with this:

txt.htmlText = randomtxt(Math.round(Math.random()\*randomtxt.length));

this chooses a random one from the array  
Math.round() rounds the number between the brackets  
Math.random() picks a random number! It works like this:  
to pick a random number between 0 and 10 you have to put it like this:  
Math.random()\*10  
it actually picks a random number between 0 and 1 and you have to multiply it by the a number!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 2, 2003, 10:37am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/3 "2003-05-02T10:37:35Z")

</div>

hey coo, tanx…

I’m now using this code:

loadText = new loadVars();  
loadText.load(“data.txt”);  
loadText.html = yes;  
//creating the loadVarsText function  
loadText.onLoad = function() {  
txt.html = true;  
txt.htmlText = randomtxt(Math.round(Math.random()\*randomtxt.length));  
randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]  
};

have a dynamic text field with the name txt

and my data.txt with:

&txt1=bla1  
&txt2=bla2  
&txt3=bla3  
&txt4=bla4

But no text is showin gup…

any idea what i’m doing wrong

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 2, 2003, 2:26pm UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/4 "2003-05-02T14:26:22Z")

</div>

switch these lines:

txt.htmlText = randomtxt(Math.round(Math.random()\*randomtxt.length));  
randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]

The creation of the array should come first cuz the thing doesn’t know where to pick the random string if no array is created yet!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 2, 2003, 2:29pm UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/5 "2003-05-02T14:29:01Z")

</div>

oeps

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 2, 2003, 2:30pm UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/6 "2003-05-02T14:30:53Z")

</div>

hmm weird still no texts is showing up:

loadText = new loadVars();  
loadText.load(“data.txt”);  
loadText.html = yes;  
//creating the loadVarsText function  
loadText.onLoad = function() {  
txt.html = true;  
randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]  
txt.htmlText = randomtxt(Math.round(Math.random()\*randomtxt.length));

};

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 2, 2003, 6:28pm UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/7 "2003-05-02T18:28:35Z")

</div>

this line:

```auto
txt.htmlText = randomtxt(Math.round(Math.random()*randomtxt.length));

```

should be:

```auto
txt.htmlText = this[randomtxt[Math.floor(Math.random()*randomtxt.length)]];

```

and…

```auto
loadText = new LoadVars();
loadText.load("data.txt");
loadText.onLoad = function() {
	var total;
	for (var vars in this) if (typeof this[vars] == "string") total++;
	txt.html=true, txt.htmlText=this["txt"+Math.ceil(Math.random()*total)];
};

```

that’s what i’d do… you can add as many variables as you wish in the txt file without having to edit the swf. =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 5, 2003, 7:45am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/8 "2003-05-05T07:45:40Z")

</div>

ok thanx man…it works!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 5, 2003, 7:51am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/9 "2003-05-05T07:51:21Z")

</div>

no problem, leenx. 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 30, 2003, 9:12am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/10 "2003-05-30T09:12:27Z")

</div>

hi another question, now i want the flash file to read the txt file from top to bottom, so no random , can anyone help?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 30, 2003, 9:34am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/11 "2003-05-30T09:34:18Z")

</div>

?? 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 30, 2003, 9:38am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/12 "2003-05-30T09:38:42Z")

</div>

great!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 30, 2003, 9:39am UTC](https://forum.kirupa.com/t/random-txt-out-of-txt-file/19876/13 "2003-05-30T09:39:17Z")

</div>

😉
