Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Numbers from a Text File

Status
Not open for further replies.

NickC111

Technical User
Sep 23, 2010
44
0
0
GB
Good Afternoon

I have a basic text file that contains rows of numbers, an example below.

700025658455
700025456633
700056656565
700354545441
700255656565

There is no limit on the amount of rows contained in the text file.

I want to create a script that will randomly pick 10 numbers from the list and output to a new text file.
 
Sounds like a homework assignment... If it is, you likely won't find much help here other than:

- Read the file into an array
- Select a random element from the array using rnd * number of elements
- Write element value to new file.

Easy. Can be done in fewer than 7 lines.

-Geates



"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hmmm. Upon further "investigation", it seems as though I have responded to your other 10 threads and have noted "this seems like a homework assignment". I apologize for any insult my previous response may have caused.

Additionally, the majority of your threads involve parsing of unusual text files. Seeing has how I am a common responder to your inquiries and often provide specific code, it is likely that a solution to your question can be concocked from those code snippets.

The above concept still remains. Put something together and post it here and we'll help work out the kinks. Again, this solution is merely a few lines long.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
NickC111: In addition to the good advice Geates gave you, here and in many other threads, it is considered courteous to reply and give some sort of feedback, if for no other reason then to let others who see your thread know if the help given worked or not. (saying "thanks" would be another reason)
 
I apologize for any insult that I may have made to my previous responses and I thank you for your advice you have given me before.

I have attempted some code but I would like some help

[set objFSO = CreateObject("Scripting.FileSystemObject")

set objInFile = objFSO.OpenTextFile("C:\test.txt", 1, true, 0)
set objOutFile = objFSO.OpenTextFile("C:\testresults.txt", 2, true, 0)

do while not (objInFile.AtEndOfStream)
strLine = objInFile.ReadLine
strRandom = rnd(strLine)
objOutFile.WriteLine strRandom
loop


objInFile.close
objOutFile.close]
 
Rnd(seed) returns a random float between 0 and 1. We want to multiple it by our max limit and round it off.

Here's how to get a random number 3 to 15.
Code:
for i = 0 to 4
   number = int(rnd() * 15) + 3
next

Notice, however, the sequence of random numbers doesn't change. Providing a seed changes that sequence but still gives us the same "random" numbers each time we run the code. Use [tt]randomize[/tt] with no seed to randomize the sequence.

Code:
[red]randomize[/red]
for i = 0 to 4
   number = int(rnd() * 15) + 3
next

Your code needs the following changes.

Code:
[red]randomize[/red]
set objFSO = CreateObject("Scripting.FileSystemObject")
set objInFile = objFSO.OpenTextFile("C:\test.txt", 1, true, 0)
set objOutFile = objFSO.OpenTextFile("C:\testresults.txt", 2, true, 0)

[green]'read the entire file[/green]
strLines = objInFile.ReadAll
[green]'break it up into an array of lines[/green]
arrLines = split(strContents, vbNewLine)
[green]'get the number of lines in the array (your upper limit)[/gree]
intUpperLimit = ubound(arrLines)

[green]'get 5 random lines between 1 and intUpperLimit and write it to a file[/green]
for = 1 to 5
   intRandom = int(rnd() * intUpperLimit) + 1
   objOutFile.WriteLine intRandom
loop

objInFile.close
objOutFile.close

Note: surround your code in [ignore]
Code:
[/ignore] tags

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Also, I don't think you insulted anyone. Guitarzan was simply pointing out commonly practiced courtisies.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks for your reply but could you look at the code you provided just to make sure it reads ok.
 
oops

Code:
randomize
set objFSO = CreateObject("Scripting.FileSystemObject")
set objInFile = objFSO.OpenTextFile("C:\test.txt", 1, true, 0)
set objOutFile = objFSO.OpenTextFile("C:\testresults.txt", 2, true, 0)

strLines = objInFile.ReadAll
arrLines = split([red]strLines[/red], vbNewLine)
intUpperLimit = ubound(arrLines)

for [red]i[/red] = 1 to 5
   intRandom = int(rnd() * intUpperLimit) + 1
   objOutFile.WriteLine intRandom
[red]next[/red]

objInFile.close
objOutFile.close

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thank you for your help but the code provides 5 random numbers not from the test file.

Example if the text file held these values

2112133
5454545
7878787
1213654
9945455
4545455
8845454
5985544
5445448
1215467
5454545
3658963
4544855
9805025


I thought the code would pick 5 random numbers from the list like below

1213654
5454545
5985544
2112133
3658963

Sorry if I am not clear




 
oops again. Apparently, I'm with it today.

Code:
for i = 1 to [red]10[/red]
   [red]index[/red] = int(rnd() * intUpperLimit) + 1
   [red]intRandom = arrLines(index)[/red]
   objOutFile.WriteLine intRandom
next

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Does it matter if you get duplicates?

The above strategy may pick the same line number from your input file multiple times. If duplicates are not desired you might try:[ul][li]read the input file into an array, randomly pick 2 items in the array and swap places. Repeat this process a significant number of times, then report the top 10 entries in the array (or bottom 10, middle 10, etc).[/li][li]create a dictionary object for the picked random numbers. Pick a random number as in previous posts, retrieve the number at that position, if it doesn't exist in the dictionary - add it, if it does exist - pick again. Repeat until there are 10 items in the dictionary object.[/li][/ul]
I think the 2nd option would be easier to implement.
 
Geates that worked perfectly - Thank you for your help as usual.
 
Sorry jges I didn't see your reply until this morning.

Your correct it does pick up duplicates and this is something I didn't require. What do you think is the best approach to acheive my results?
 
Here's some code that builds on what Geates has done:

Code:
randomize  
[COLOR=blue]set[/color] objFSO [COLOR=blue]=[/color] CreateObject("Scripting.FileSystemObject")  
[COLOR=blue]Set[/color] numberDic [COLOR=blue]=[/color] CreateObject("Scripting.Dictionary")  
[COLOR=blue]set[/color] objInFile [COLOR=blue]=[/color] objFSO.OpenTextFile("C:\Temp\test.txt", 1, true, 0)  
[COLOR=blue]set[/color] objOutFile [COLOR=blue]=[/color] objFSO.OpenTextFile("C:\Temp\testresults.txt", 2, true, 0)  

strLines [COLOR=blue]=[/color] objInFile.ReadAll  
arrLines [COLOR=blue]=[/color] split(strLines, vbNewLine)  
intUpperLimit [COLOR=blue]=[/color] ubound(arrLines)  
numPicks [COLOR=blue]=[/color] 10  

[COLOR=green]'number of random picks must be less than or[/color]
[COLOR=green]'equal to the number of lines in the input file[/color]
[COLOR=blue]if[/color] intUpperLimit < numPicks [COLOR=blue]then[/color]  
	numPicks [COLOR=blue]=[/color] intUpperLimit  
end [COLOR=blue]if[/color]  

[COLOR=blue]Do[/color] [COLOR=blue]Until[/color] numberDic.Count [COLOR=blue]=[/color] numPicks  
	index [COLOR=blue]=[/color] int(rnd() [COLOR=blue]*[/color] intUpperLimit) [COLOR=blue]+[/color] 1  
	intRandom [COLOR=blue]=[/color] arrLines(index)  
	intRandom [COLOR=blue]=[/color] Trim(intRandom)  
	  [COLOR=green]'if blank lines exist in text file, don't add them[/color]
	if intRandom <> "" [COLOR=blue]then[/color]  
		  [COLOR=green]'if the line chosen is not in the dictionary object, add it[/color]
		if [COLOR=blue]not[/color] numberDic.exists(intRandom) [COLOR=blue]then[/color]  
			numberDic.Add intRandom, intRandom  
		end [COLOR=blue]if[/color]  
	end [COLOR=blue]if[/color]  
[COLOR=blue]Loop[/color]  

[COLOR=blue]for[/color] [COLOR=blue]each[/color] item [COLOR=blue]in[/color] numberDic  
	objOutFile.WriteLine item  
[COLOR=blue]next[/color]  

objInFile.close  
objOutFile.close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top