topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 1:00 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: DONE: A script to display a random line from txt file  (Read 17631 times)

dreats

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
DONE: A script to display a random line from txt file
« on: February 20, 2006, 03:44 AM »
This may seem useless to most, so if it's too difficult to make I am willing to donate a few coins, if it is allowed, to spur some motivation.

I need a script that will work something like this: once executed it will output a random line from a specified text file.

Except, there would be a little probability involved.

Each line in this text file is specially formatted in a particular syntax: "<NUMBER>, <TEXT>". The number represents the probability of the text to display in cumulative percentage.

Take for example this txt file with these two lines in it:

10, helloworld
10, foobar

50/50 chance either would be displayed. In contrast, if it was:

518572, helloworld
10, foobar

99.9% you'll see "helloworld".

Let me know how feasible it is. I will donate to either the site or the coder. 15-20$ depending on difficulty sounds ok? I really don't know.
« Last Edit: March 07, 2006, 01:38 AM by brotherS »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #1 on: February 20, 2006, 04:02 AM »
easy to do - i'll let someone else do it because its a fun project, but if no one responds in 24 hours ill post a solution.
this is actually a fun programming challenge that i would give to a new programmer, so it's a good post for people just learning how to program :)

im assuming you want an .exe program for windows that does this right? if not, if you want it done in a particular language then you should post an update.

dreats

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #2 on: February 20, 2006, 04:46 AM »
Great, thanks for your quick response!

As long as we're already doing this, and I'll be donating to either the site or the coder, why not kick it up a notch? :)

I was thinking maybe adding an editable textfield inside the msgbox or window where this random line would appear. to make it easier to change the probability of it appearing next time...

Let me know if you have any suggestions. i by the way would be using it as a nifty todo list utility, helping me decide what to do next.

EDIT: Yeah and language doesn't matter. exe, ahk, vbs, just let it work on windows.
« Last Edit: February 20, 2006, 05:00 AM by dreats »

Kniht

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 8
    • View Profile
    • Qxxy.com
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #3 on: February 20, 2006, 06:45 AM »
Here's a short program. It accepts input from stdin in roughly the format you gave. (It just reads an integer, ignores 2 characters for comma and space, and reads the rest of the line for the string.) I don't have a compiler setup to distribute windows executables, so you'll have to compile this or find someone who can.

#include <cstdlib>
#include <iostream>
#include <vector>
#include <utility>
#include <string>

double random() { return std::rand() / ( RAND_MAX + 1.0 ); }

int main() {
using namespace std;

srand( time(0) );
rand(); rand(); rand();

typedef unsigned long ulong;
typedef vector< pair< ulong, string > > Data;

Data data;
ulong sum = 0;

for( Data::value_type input;
cin >> input.first &&
cin.ignore(2) &&
getline( cin, input.second );
) {
data.push_back( input );
sum += input.first;
}

{
ulong which = ulong( random() * sum );
for ( Data::const_iterator n = data.begin(), end = data.end(); n != end; ++n ) {
if ( which < n->first ) {
cout << n->second;
break;
}
which -= n->first;
}
}

cout << '\n';
return 0;
}

// this code is released into the public domain for those who care

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #4 on: February 20, 2006, 10:02 AM »
The following code in ahk should solve your problem:

.exe version
.ahk version

Attached to this post, i have added a compressed file with this code in a file, and the compiled version of it.
Only one remark: the compiled version has no possibility of changing the settings, so, it can only accept one file named "file.txt", and in the same folder that the program is.
I hope this fits your needs!
Any problem, please post below ;)
« Last Edit: May 02, 2006, 04:45 PM by jgpaiva »

dreats

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #5 on: February 20, 2006, 03:51 PM »
Hey it works. Better than I could ask for actually... ignoring lines without the syntax and everything.

I made a few little changes to the GUI to make it more windows-like.

Just one thing, could you add a little tag before the text, saying the real percentage, in 100% base denominator? So the text would read "[50%] foobar" instead of just "foobar"...

Thank you for this.

#NoTrayIcon

FileToRead = Todo.txt ;replace "FILE.txt" by the name of the file to read
Sum = 0
AutoTrim,Off
Loop, Read, %FileToRead%,,
{
  Flag=false
  Loop, parse, A_LoopReadLine, %A_Tab%
  {
    If Flag=true
      continue
    Sum +=%A_LoopField%
    Flag=true
  }
}
Random, Number, 1, Sum
Sum = 0
Line = 0
Loop, Read, %FileToRead%,,
{
  Line+=1
  Flag=false
  Loop, parse, A_LoopReadLine, %A_Tab%
  {
    If Flag=true
      If Sum>=%Number%
      {
Message=%A_LoopField%
        GoSub, Found
        Return
      }     
    Sum +=%A_LoopField%
    Probability=%A_LoopField% 
    Flag=true

  }
}
return
 
Found:
  Gui, Add, Picture, x11 y11 Icon5, user32.dll
  Gui, Add, Button, gSave x78 y66 w75 h23 +default,&OK
  Gui, Add, Button, gRetry x158 y66 w75 h23,&Retry
  Gui, Add, Button, gGuiClose x239 y66 w75 h23 , &Cancel
  Gui, Add, Edit, vNewProbability x10 y58 w32 h20 Limit4,%Probability%
  Gui, Font,  s12 w700, Verdana
  Gui, Add, Text, x61 y10 w327 h38,%Message%
  Gui, Show, xCenter yCenter h101 w395, RandomLine
Return

GuiClose:
ExitApp

Save:
  Gui,Submit
  FileRead,CompleteFile,%FileToRead%
  StringReplace,CompleteFileModified,CompleteFile,``n,`n,ReplaceAll
  StringReplace,CompleteFile,CompleteFileModified,%probability%%A_Tab%%message%,%NewProbability%%A_Tab%%message%
  FileDelete,%FileToRead%
  FileAppend,%CompleteFile%,%FileToRead%
  ExitApp

Retry:
Reload

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #6 on: February 20, 2006, 04:44 PM »
I didn't quite understand what you meant, but please check if what you're looking for is something like this:

.exe version
.ahk version
« Last Edit: May 02, 2006, 04:46 PM by jgpaiva »

dreats

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #7 on: February 20, 2006, 05:05 PM »
Precisely.

It is complete and working better than expected.

A BIG THANK YOU GOES TO JGPAIVA!

I'll be donating to the site later this week. What a great community.

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #8 on: February 20, 2006, 05:40 PM »
You're welcome ;)

I'll be donating to the site later this week. What a great community.
:Thmbsup: :Thmbsup: :Thmbsup:

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: A script to display a random line from txt file
« Reply #9 on: February 20, 2006, 06:10 PM »
jgpaiva, and all other people who write coding snacks and such:

set yourself up a little paypal button so that people can show their appreciation to you if you do little projects like this:
https://www.donation...dex.php?topic=1283.0

it's wonderful when people donate to the site, but this forum is also about people donating to each other to help each other out, so you guys and gals should have a way to accept donations!