One of n lines in a file: Difference between revisions
Content added Content deleted
Line 233: | Line 233: | ||
9 100154 |
9 100154 |
||
10 99880 |
10 99880 |
||
=={{header|Batch File}}== |
|||
I chose to only run this 100,000 times as it is slightly time consuming (running at 105 simulations / second). |
|||
I attempted to optimise a few ways but none were especially helpful. |
|||
There is also no need to parse the actual number of lines in the file to <code>one_of_n</code> |
|||
'''Batch file to call one_of_n''' |
|||
<lang dos> |
|||
@echo off |
|||
for /l %%i in (1,1,10000) do call one_of_n |
|||
:: To show progress add to the FOR loop code block - |
|||
:: title %%i |
|||
for /l %%i in (1,1,10) do ( |
|||
for /f "usebackq tokens=1,2 delims=:" %%j in (`find /c "%%i" output.txt`) do echo Line %%i =%%k |
|||
) |
|||
del output.txt |
|||
pause>nul |
|||
</lang> |
|||
'''one_of_n''' |
|||
<lang dos> |
|||
setlocal enabledelayedexpansion |
|||
set /a count=1 |
|||
for /f "tokens=*" %%i in (file.txt) do ( |
|||
set /a rand=!random! %% !count! |
|||
if !rand!==0 set line=!count! |
|||
set /a count+=1 |
|||
) |
|||
echo %line% >> output.txt |
|||
endlocal |
|||
exit /b |
|||
</lang> |
|||
'''Output''' |
|||
<pre> |
|||
Line 1 = 9,868 |
|||
Line 2 = 10032 |
|||
Line 3 = 9988 |
|||
Line 4 = 10160 |
|||
Line 5 = 10189 |
|||
Line 6 = 9953 |
|||
Line 7 = 9851 |
|||
Line 8 = 10052 |
|||
Line 9 = 9937 |
|||
Line 10 = 9970 |
|||
</pre> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |