Welcome Guest.   Make a donation to an author on the site August 23, 2014, 02:50:37 PM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Read the full one-year retrospective report on DonationCoder.com.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: DONE: A script to display a random line from txt file  (Read 7656 times)
dreats
Participant
*
Posts: 4

View Profile Give some DonationCredits to this forum member
« on: February 20, 2006, 03:44:52 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:

[copy or print]
10, helloworld
10, foobar

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

[copy or print]
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:12 AM by brotherS » Logged
mouser
First Author
Administrator
*****
Posts: 33,260



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #1 on: February 20, 2006, 04:02:26 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 smiley

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.
Logged
dreats
Participant
*
Posts: 4

View Profile Give some DonationCredits to this forum member
« Reply #2 on: February 20, 2006, 04:46:01 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? smiley

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.


* untitled2.GIF (2.9 KB, 379x199 - viewed 570 times.)
« Last Edit: February 20, 2006, 05:00:36 AM by dreats » Logged
Kniht
Charter Member
***
Posts: 8

View Profile WWW Give some DonationCredits to this forum member
« Reply #3 on: February 20, 2006, 06:45:35 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.

[copy or print]
#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
Logged
jgpaiva
Global Moderator
*****
Posts: 4,710



Artificial Idiocy

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #4 on: February 20, 2006, 10:02:54 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 Wink
« Last Edit: May 02, 2006, 04:45:51 PM by jgpaiva » Logged

dreats
Participant
*
Posts: 4

View Profile Give some DonationCredits to this forum member
« Reply #5 on: February 20, 2006, 03:51:44 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.

[copy or print]
#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


* untitled.PNG (5.43 KB, 401x126 - viewed 542 times.)
Logged
jgpaiva
Global Moderator
*****
Posts: 4,710



Artificial Idiocy

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #6 on: February 20, 2006, 04:44:31 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:17 PM by jgpaiva » Logged

dreats
Participant
*
Posts: 4

View Profile Give some DonationCredits to this forum member
« Reply #7 on: February 20, 2006, 05:05:28 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.
Logged
jgpaiva
Global Moderator
*****
Posts: 4,710



Artificial Idiocy

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #8 on: February 20, 2006, 05:40:10 PM »

You're welcome Wink

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

mouser
First Author
Administrator
*****
Posts: 33,260



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #9 on: February 20, 2006, 06:10:20 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:
http://www.donationcoder....um/index.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!
Logged
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.039s | Server load: 0.02 ]