How to randomise lines in a text file?

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
Stark
Emperor's Hand
Posts: 36169
Joined: 2002-07-03 09:56pm
Location: Brisbane, Australia

How to randomise lines in a text file?

Post by Stark »

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?
User avatar
Stark
Emperor's Hand
Posts: 36169
Joined: 2002-07-03 09:56pm
Location: Brisbane, Australia

Post by Stark »

Bah, the internet is full of line-randomising programming examples, but they're all greek to me. How am I supposed to use this?
User avatar
brianeyci
Emperor's Hand
Posts: 9815
Joined: 2004-09-26 05:36pm
Location: Toronto, Ontario

Post by brianeyci »

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.

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");
	}
}
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
User avatar
Stark
Emperor's Hand
Posts: 36169
Joined: 2002-07-03 09:56pm
Location: Brisbane, Australia

Post by Stark »

Do you take russian girls? It'd be cheaper for me to put one on zoo class and deal with my staggering debt that way. :)

I ...er, I've got the JRE. I don't know how to give it code, though. A .java file maybe?
User avatar
Xon
Sith Acolyte
Posts: 6206
Joined: 2002-07-16 06:12am
Location: Western Australia

Post by Xon »

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!
"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.
User avatar
brianeyci
Emperor's Hand
Posts: 9815
Joined: 2004-09-26 05:36pm
Location: Toronto, Ontario

Post by brianeyci »

You have no idea how much pain you caused me Stark.

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");
	}

}

}
Here is the .class file for Windows.

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
Now I know why I quit programming :twisted:. Good night,

Brian
User avatar
Stark
Emperor's Hand
Posts: 36169
Joined: 2002-07-03 09:56pm
Location: Brisbane, Australia

Post by Stark »

Thanks for all your help guys! Strange how something so trivial can be so hard when you're not ... um... having a clue.
Post Reply