Random Sort-opens list, rewrites random list

Community Forums/Showcase/Random Sort-opens list, rewrites random list

CodeD(Posted 2005) [#1]
Here is a little program I came up with because Open Office didn't have an option to do a random sort, which is what I needed.

It will take any plain text list file, jumble the list, 1 million times (less than a second!), then write the new file to text.

It's pretty simple and kinda dirty but, I did it to familiarize myself a bit with types, data reading, and file reading/writing, which are some of my weak points.

the text prompts are in there because, i was having program errors and I was trying to isolate the problem.

;Random Name Sort Program
;by Richard Colletta 2-3-2005
;COPYRIGHT (C) 2005
;Get your own code!!

;Copy and Paste Last Name Column from Spreadsheet into notepad/plain text
;One name per line
;the last line must read STOP
;Save file as names.txt

Graphics 800, 600, 32, 2

SeedRnd (MilliSecs()) ;randomize the timer with the internal clock

;variable for checking end of file
rdata = True

;Create Type for data sorting
Type Pnames
Field Name$
End Type

;Input file name to open

Print "Random Name Sort"
Print "by Richard Colletta 2-3-2005"
Print "richard.colletta@..."

fname$ = Input$("File to Open?")

;open the file for reading

filein = ReadFile (fname$)

Print "File Loaded! press any key.": WaitKey()

;Read the names from the file
While rdata
name$ = ReadLine (filein)
If name$ = "STOP";read this line for end of file
rdata = False
Else
people.pnames = New pnames
If people.pnames <> Null
people\name$ = name$
rng = Rand(4) ; return random integer 1 - 4
If rng = 1 Then Insert people Before First pnames
If rng = 2 Then Insert people After First pnames
If rng = 3 Then Insert people Before Last pnames
If rng = 4 Then Insert people After Last pnames
Print "DATA:  " + name$
Else
Text 0,0, "Program Error!" ;error checking, type instance could not be created
rdata = False
EndIf
EndIf
Wend

Print "randomizing! press any key."
WaitKey()
Print "RANDOMIZING VARIABLES --> 1 MILLION ITERATIONS"

For x = 1 To 1000000
For people.pnames = Each pnames 
rng = Rand(4) ; return random integer 1 - 4
If rng = 1 Then Insert people Before First pnames
If rng = 2 Then Insert people After First pnames
If rng = 3 Then Insert people Before Last pnames
If rng = 4 Then Insert people After Last pnames

;Print "RANDOM SORTING " + people\name$
Next
Next

Print "done! press any key.": WaitKey()

CloseFile (fname$)

Print "Checking Data. press any key.": WaitKey()
For people.pnames = Each pnames
Print people\name$
Next

Print "Writing Random Sort List to File. press any key.": WaitKey()

fileout = WriteFile ("randomsort.txt")
For people.pnames = Each pnames
WriteString ( fileout, people\name$ + Chr$(13) )
Next
CloseFile (fileout)

Print "Random List written to file randomsort.txt!!"
Print "Colletta definitely deserves a promotion for this. press any key to end."
WaitKey()

End 



jfk EO-11110(Posted 2005) [#2]
What do you need a random sort list function for in iraq? Let me guess, it's the "who's gonna buy some cigs at the other end of the town" thing? Take care.


jfk EO-11110(Posted 2005) [#3]
Sorry for being kind of unsensitive - are you ok? give us a ping from time to time.


CodeD(Posted 2006) [#4]
this reply is late but, i hate to leave things undone. In short, it was a 'lottery' type give-a-away and excell and open office had no random sort function. So, I made one.