ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Finished Programs

DONE: A script to display a random line from txt file

(1/2) > >>

dreats:
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.

mouser:
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:
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.

Kniht:
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:
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 ;)

Navigation

[0] Message Index

[#] Next page

Go to full version