# Pick random line from text file

**URL:** <https://discourse.nodered.org/t/pick-random-line-from-text-file/73560>\
**Category:** General\
**Created:** [11 January 2023 19:46 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560 "2023-01-11T19:46:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GTM](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@GTM](https://discourse.nodered.org/u/GTM)\
**Post date:** [11 January 2023 19:46 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560/1 "2023-01-11T19:46:33Z")

</div>

Hi all, I have a text file of jokes, each joke is on a new line in the text file. How can I get node red to pick a random line from the file.

I will be sending the result to a TTS service, I have done this before using an API that served a random joke but how do I get it working using a text file.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 January 2023 20:00 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560/2 "2023-01-11T20:00:35Z")

</div>

Use the random note to grab a random number then take your text file and use a split note to split it based on the\in character and create an array out of it. Then you could use the random number to pick them up array the line from the array based on that.

---

<div class="post-metadata">

**Author:** ![GTM](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@GTM](https://discourse.nodered.org/u/GTM)\
**Post date:** [12 January 2023 19:35 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560/3 "2023-01-12T19:35:36Z")

</div>

I did manage to get this working using a read file node going to a function node to split into an array and pick a random number from the array.

```auto
var joke = msg.payload.split("~");
var rnd = Math.floor(Math.random() * joke.length);
msg.payload = joke[rnd]
return msg;

```

I took all the line breaks / new line stuff out the text file and replaced them with "~". I just couldn't find anything that worked to split by new line. Tried "/n" (Still learning this lol)

This does work, but it loads the text file in full every time. Is there a better way of doing this?? or is this way acceptable??

Any help, criticism of my crummy code greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 January 2023 20:27 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560/4 "2023-01-12T20:27:15Z")

</div>

> [@GTM](#):
>
> Tried "/n"

It should be `"\n"`.

---

<div class="post-metadata">

**Author:** ![GTM](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@GTM](https://discourse.nodered.org/u/GTM)\
**Post date:** [12 January 2023 21:00 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560/5 "2023-01-12T21:00:11Z")

</div>

Yea I tried /n \n all different things, I think the txt file might have come from excel or csv, I couldn't find anything that worked lol

I did try using the split node but was complaining about some unrecognised characters as well, looked fine to me just looked like a normal txt file.

---

<div class="post-metadata">

**Author:** ![Hjorth-Jensen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hjorth-jensen/32/74298_2.png) [@Hjorth-Jensen](https://discourse.nodered.org/u/Hjorth-Jensen)\
**Post date:** [13 January 2023 10:12 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560/6 "2023-01-13T10:12:03Z")

</div>

Hi GTM  
I use this code, and it works fine for me.

```auto
[{"id":"a059aeeda27bd133","type":"csv","z":"40351d051417302b","name":"","sep":".","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":970,"y":1980,"wires":[["c6f712e6d57b84fa"]]},{"id":"38f1b2d4755bcc29","type":"file in","z":"40351d051417302b","name":"","filename":"godmorgen.csv","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":780,"y":1980,"wires":[["a059aeeda27bd133"]]},{"id":"c6f712e6d57b84fa","type":"function","z":"40351d051417302b","name":"function 2","func":"var array = msg.payload; // Definerer arrayet fra payload-objektet\nvar randomIndex = Math.floor(Math.random() * array.length); // Vælger et tilfældigt index i arrayet\nvar randomElement = array[randomIndex]; // Vælger det element, der er på det tilfældige index\n\nmsg.payload = randomElement; // Gemmer det tilfældige element i payload-objektet\nreturn msg; // Returnerer beskeden","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1100,"y":1980,"wires":[["d0100b8050cffd27"]]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 March 2023 10:12 UTC](https://discourse.nodered.org/t/pick-random-line-from-text-file/73560/7 "2023-03-14T10:12:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
