Sign in to follow this  
Followers 0
Read

Random number in file

17 posts in this topic

How to make random combinations script

Ex.

test.txt is number combinations

01 02 03

01 02 04

01 02 05

test.bin coming random combinations

01 02 05

01 02 03

01 02 04

$sData = FileRead(@ScriptDir & "\test.txt")
$random = // random code here!!! How?
$hFile = FileOpen(@ScriptDir & "\test.bin", 18)
FileWrite($hFile, $random)
FileClose($hFile)

Eng bad :)

Share this post


Link to post
Share on other sites



Read,

How to make random combinations

Did you look to see if Random appeared in the Help file? Probably not or you would have found it. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

test.bin come always "NULLNULLČ»¢Q9@" -something is very wrong.

maybe help?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

That could be correct, but without any code it can be a little difficult to diagnose the problem :)

Edit: Check the flags for FileOpen()

Global $sRandom = Random(0, 100)
Global $hFile = FileOpen(@ScriptDir & "\Test.bin", 2)
FileWrite($hFile, $sRandom)
FileClose($hFile)
Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

What is it > 59.7735021729022 ?

test.bin must coming random combinations - take test.txt comb. and make random in test.bin

one example of 100 - not work

$sData = FileRead(@ScriptDir & "\test.txt")
$random1 = Random($sData,0)
$hFile = FileOpen(@ScriptDir & "\test.bin", 18)
FileWrite($hFile, $random1)
FileClose($hFile)
Edited by Read

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

If I have understood your first post correctly, then I think this may be what you are looking for.

#include <file.au3> 


local $aData,$MaxRow,$rmd,$temp,

_FileReadToArray(@ScriptDir & "\test.txt", $aData)
$MaxRow = UBound($aData) - 1
For $i = 1 To $MaxRow
    $rnd = Random(1, $MaxRow, 1)
    $temp = $aData[$i]
    $aData[$i] = $aData[$rnd]
    $aData[$rnd] = $temp
Next
$hFile = FileOpen(@ScriptDir & "\test.bin", 18)
For $i = 1 To $MaxRow
    FileWrite($hFile, $random1)
Next
FileClose($hFile)

Edit: fixed bug in script

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Bowmore!

Edit only: #include <arrsy.au3> >> #include <array.au3>

Error:

Posted Image

Array.au3 don't have _FileReadToArray but File.au3 is it - both not work

Share this post


Link to post
Share on other sites

Script in previous post has now been fixed.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

What is it &gt; 59.7735021729022 ?

The Random() function has flags too. Change

$random1 = Random($sData,0)

to

$random1 = Random($sData,1)

<strike>or</strike> and, read thru the Help file some more.

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Random ( [Min [, Max [, Flag]]] )

Parameters

Min [optional] The smallest number to be generated. The default is 0.

Max [optional] The largest number to be generated. The default is 1.

Flag [optional] If this is set to 1 then an integer result will be returned. Default is a floating point number.


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Not work.

Error:

Posted Image

Script in previous post has now been fixed.

Share this post


Link to post
Share on other sites

You should be capable of fixing that yourself. Just remove the stray comma at the end of the line as indicated by the error message.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Can't :)

Something is wrong here or no: FileWrite($hFile, $random1)???

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

$random1 looks to be a code mistake.

Replace it with:

$aData[$i]

Edit:

After looking at that code, I don't see you still getting the results you want.

There was no exception made for carriage return/Line feeds.

And it's still reading an "ansi" text file, then writing it to a binary file... not sure why we'd do that.

Edited by SmOke_N
missed code tag

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Yes, not work.

What would be a working code?

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Yes, not work.

What would be a working code?

The working solution could happen much faster if you took the time and did some tutorials.

You are showing us no desire to either read the help file, comprehend it, or both.

http://www.autoitscript.com/wiki/FAQ#Where_can_I_learn_AutoIt.3F_Are_there_any_tutorials.3F

Keep in mind, anyone that helps you here is because they want to. There are no have to's.

I assume this is what you're after.

#region Example only data
Global $gs_filefrom = @ScriptDir & "\test.txt"
Global $gs_fileto = @ScriptDir & "\test.bin"

If Not _my_FileToRandomLines($gs_filefrom, $gs_fileto) Then
    Switch @error
        Case 1
            MsgBox(16 + 262144, "Error", "Error opening " & $gs_filefrom)
        Case 2
            MsgBox(16 + 262144, "Error", "Empty file " & $gs_fileto)
        Case 3
            MsgBox(16 + 262144, "Error", "Error opening " & $gs_fileto)
    EndSwitch
    Exit
EndIf
#region Example only data

; Param 1: The file to read from
; Param 2: The file to write to
; Param 3: The read flag ( default is 0 for reading standard text file )
; Param 3: The write flag ( default is 2 for erasing old content and creating a new standard text file )
Func _my_FileToRandomLines($s_filein, $s_fileout, $i_readflag = 0, $i_writeflag = 2)

    If $i_readflag = -1 Or $i_readflag = Default Then $i_readflag = 0
    If $i_writeflag = -1 Or $i_writeflag = Default Then $i_writeflag = 2

    ; open with reader flag mode
    Local $h_open = FileOpen($s_filein, $i_readflag)
    If $h_open = -1 Then Return SetError(1, 0, 0)

    Local $s_fread = FileRead($h_open)
    FileClose($h_open)

    If $s_fread = "" Then Return SetError(2, 0, 0)

    ; open write to file
    $h_open = FileOpen($s_fileout, $i_writeflag)
    If $h_open = -1 Then Return SetError(3, 0, 0)

    ; split file lines
    Local $a_lines = StringSplit(StringStripCR($s_fread), @LF)

    ; shuffle lines, start at index 1, index 0 is only count
    Local $a_writelines = _ArrayShuffle($a_lines, 1)

    For $iline = 1 To $a_writelines[0]
        FileWriteLine($h_open, $a_writelines[$iline])
    Next

    FileClose($h_open)

    Return 1
EndFunc

Func _ArrayShuffle($a_arr, $i_lb = 0)
    Local $i_ub = UBound($a_arr) - 1

    Local $v_tmp, $i_rndm
    For $irow = $i_lb To $i_ub
        $v_tmp = $a_arr[$irow]
        $i_rndm = Random($i_lb, $i_ub, 1)
        $a_arr[$irow] = $a_arr[$i_rndm]
        $a_arr[$i_rndm] = $v_tmp
    Next

    Return $a_arr
EndFunc
Edited by SmOke_N
brainfart

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

works very well, thanks SmOke_N!

Thanks all :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0