# How to generate combinations of 0 and 1?

## Recommended Posts

Hello

Could anyone tell me by what logic is it possible to generate combinations of 1s and 0s for given length?

examples:

1:

1

0

2:

11

10

01

00

3:

111

110

100

101

011

010

001

000

It looks like n bit binary number.

edited

##### Share on other sites

Random perhaps? Though surprised as this should be simple for you, unless I misunderstood?

```ConsoleWrite(_RandomBinaryString() & @CRLF)
ConsoleWrite(_RandomBinaryString(2) & @CRLF)
ConsoleWrite(_RandomBinaryString(3) & @CRLF)

Func _RandomBinaryString(\$iLength = Default) ; Returns a string.
If StringIsInt(\$iLength) Then \$iLength = Int(\$iLength)
If \$iLength <= 0 Or \$iLength = Default Then \$iLength = 1
Local \$sReturn = ''
For \$i = 1 To \$iLength
\$sReturn &= String(Random(0, 1, 1))
Next
Return \$sReturn
EndFunc   ;==>_RandomBinaryString```

##### Share on other sites

Random perhaps?

Lucky me. I got this :

```1
11
111```

I think he wants to generate all possibilities, which makes the script more complex, I'm working on it.

Br, FireFox.

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

##### Share on other sites

I invented this but I somehow have to return it instead of writing to console. This seems to be little more tricky

```func MakeTable(\$iLen,\$Combinations = "")
For \$i = 0 to 1
If \$iLen = 0 Then
ConsoleWrite(\$Combinations&@CRLF)
Return
else
MakeTable(\$iLen-1,\$Combinations&\$i)
EndIf
Next
EndFunc```
Edited by E1M1

edited

##### Share on other sites

AND... Here you go ! :

```#include <Array.au3>
#include <String.au3>

Global \$iLength = 10, \$aBytes[2 ^ \$iLength]

For \$iIndex = 0 To 2 ^ \$iLength -1
\$aBytes[\$iIndex] = Integer2Binary(\$iIndex, \$iLength)
Next

_ArrayDisplay(\$aBytes) ;sorted

; scramble it
For \$i = 2 ^ \$iLength - 1 To 1 Step -1
\$new = Random(0, \$i, 1)
\$tmp = \$aBytes[\$i]
\$aBytes[\$i] = \$aBytes[\$new]
\$aBytes[\$new] = \$tmp
Next

_ArrayDisplay(\$aBytes) ;what you want

Func Integer2Binary(\$in, \$len) ;coded by UEZ, edited by FireFox
If \$in = 0 Then Return StringFormat("%0" & \$len & "s", 0)
Local \$bin
While \$in > 0
\$bin &= Mod(\$in, 2)
\$in = Floor(\$in / 2)
WEnd
\$bin = _StringReverse(\$bin)
\$bin = StringFormat("%0" & \$len & "s", \$bin)
Return (\$bin)
EndFunc   ;==>Integer2Binary```

Br, FireFox.

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

##### Share on other sites

Huge thanks to you.

edited

##### Share on other sites

Ah, now I get it FireFox. I read it too quickly and just thought they were wanting to create a random 'binary string.' Cheers.

##### Share on other sites

Huge thanks to you.

You're welcome

Ah, now I get it FireFox. I read it too quickly and just thought they were wanting to create a random 'binary string.' Cheers.

I got it, cheers too

Br, FireFox.

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

## Create an account

Register a new account