How to randomise lines in a text file?
Moderator: Thanas
How to randomise lines in a text file?
I've made a text file for new starnames in GalCiv2, but I've recently discovered the starnames aren't particularly random. I'd like to give the game a hand by randomising my currently alphabetic list of names - is there some quick way to do this?
Bah, the internet is full of line-randomising programming examples, but they're all greek to me. How am I supposed to use this?
Okay for you Stark I will go back in time three years and program even though I hate it. And because it's a piece of cake and nobody'll bother to do it for you because it's so easy.
Half an hour later I have this code. Half of it is ripped from the net, okay nearly all of it, but I came up with the algorithm. Which by the way sucks because it doesn't work if there's two of the same name but oh well. A real programmer can probably come up with something in five lines, but hey you pay for what you get. If you have a java compiler you could cut and paste the code in and it might work.
Knowing this place, they'll be five or six people coming in who can do it in five lines or less. But oh well. You owe me 500 megs for installing the development kit, and 5000000000000000000000000000 USD, no AUS crap. I also take blowjobs. Either that or a thank you.
Standalone coming in half an hour if the above means gibberish.
Brian
Half an hour later I have this code. Half of it is ripped from the net, okay nearly all of it, but I came up with the algorithm. Which by the way sucks because it doesn't work if there's two of the same name but oh well. A real programmer can probably come up with something in five lines, but hey you pay for what you get. If you have a java compiler you could cut and paste the code in and it might work.
Code: Select all
import java.io.*;
import java.util.Random;
public static void main(String[] args){
if (args.length == 2){
FileInputStream fstream = new FileInputStream(args[0]);
DataInputStream in = new DataInputStream(fstream);
Random randomNumber = new Random();
int tempNumber = 0;
int totalLines = 0;
while(in.available() != 0)
{
totalLines += 1;
}
in.close();
String[] outputArray = new String[totalLines];
for (i = 0; i < totalLines; i++)
{
outputArray[i] = "";
}
in = new DataInputStream(fstream);
for (i = 0; i < totalLines; i++){
temp = in.readLine();
while(outputArray[tempNumber].compareTo(temp) == 0){
tempNumber = randomNumber.nextInt(totalLines);
}
outputArray[tempnumber] = temp;
}
FileOutputStream out;
PrintStream p;
out = new FileOutputStream(args[1]);
p = new PrintStream(out);
for (i = 0; i < totalLines; i++){
p.println(outputArray[i]);
}
p.close();
}
else {
System.out.println("randomize.java input.txt output.txt");
}
}
Standalone coming in half an hour if the above means gibberish.
Brian
You got Excel?
Point at excel and tell it to parse each line into a row. Select row, randomize! Save back in the same format.
Profit!
Point at excel and tell it to parse each line into a row. Select row, randomize! Save back in the same format.
Profit!
"Okay, I'll have the truth with a side order of clarity." ~ Dr. Daniel Jackson.
"Reality has a well-known liberal bias." ~ Stephen Colbert
"One Drive, One Partition, the One True Path" ~ ars technica forums - warrens - on hhd partitioning schemes.
"Reality has a well-known liberal bias." ~ Stephen Colbert
"One Drive, One Partition, the One True Path" ~ ars technica forums - warrens - on hhd partitioning schemes.
You have no idea how much pain you caused me Stark.
Here is the corrected code.
Here is the .class file for Windows.
Since you have JRE, to run it, go to command line and you can run it with
Now I know why I quit programming . Good night,
Brian
Here is the corrected code.
Code: Select all
import java.io.*;
import java.util.*;
class RandomizeStark {
public static void main(String[] args){
if (args.length == 2){
try {
FileReader fstream = new FileReader(args[0]);
BufferedReader in = new BufferedReader(fstream);
Random randomNumber = new Random();
String temp = "";
int i = 0;
int tempNumber = 0;
int totalLines = 1;
temp = in.readLine();
while(in.ready())
{
totalLines += 1;
System.out.println("Randomizing : " + in.readLine());
}
in.close();
String[] outputArray = new String[totalLines];
for (i = 0; i < totalLines; i++)
{
outputArray[i] = "";
}
fstream = new FileReader(args[0]);
in = new BufferedReader(fstream);
for (i = 0; i < totalLines; i++){
temp = in.readLine();
tempNumber = randomNumber.nextInt(totalLines);
while(!(outputArray[tempNumber].equals(""))){
tempNumber = randomNumber.nextInt(totalLines);
}
outputArray[tempNumber] = temp;
}
FileOutputStream out;
PrintStream p;
out = new FileOutputStream(args[1]);
p = new PrintStream(out);
for (i = 0; i < totalLines; i++){
p.println(outputArray[i]);
}
p.close();
} catch (Exception e) {
System.out.println("Fucked up.");
}
}
else {
System.out.println("java RandomizeStark input.txt output.txt");
}
}
}
Since you have JRE, to run it, go to command line and you can run it with
Code: Select all
java RandomizeStark input.txt output.txt
Brian