Sign in to follow this  
Followers 0
Kyan

Random number generation in a interval and koda designer window style

16 posts in this topic

#1 ·  Posted (edited)

Hi

I want to generate a random number could be 0 to 99999 with random command (in this example is just 0 to 9), and if the number is already been used, the random command is called again until another number not already shown was generated

I write it in this way (the vertical bars are used to separate different numbers)

$store = ''
$ts = TimerInit()
Do
$ran = Random(0,9,1)
If StringInStr($store,$ran,0,1) = 0 Then
$store &=$ran & "|"
ConsoleWrite($store & @CRLF)
EndIf
Until StringLen($store) = 20
ConsoleWrite("Time:" & TimerDiff($ts))
Exit

I don't know if it could be done faster, or in a better way

i got this output:

5|
5|4|
5|4|0|
5|4|0|2|
5|4|0|2|1|
5|4|0|2|1|9|
5|4|0|2|1|9|7|
5|4|0|2|1|9|7|3|
5|4|0|2|1|9|7|3|6|
5|4|0|2|1|9|7|3|6|8|
Time:0.832828704999992

The other question about koda designer, is about that magnetic effect of tool's windows, when I create a styled tool windows, I won't get that effect (and yes, I set the parent window when created the tool window :) )

thanks in advance :)

EDIT: In the random loop I test it out with the interval 0->140 and it stops at this point:

12|43|33|121|134|78|68|35|129|48|114|102|92|83|59|56|38|31|18|108|54|107|36|57|128|120|44|118|97|105|41|10|139|81|15|104|80|47|16|96|88|19|111|79|65|127|110|137|45|58|74|82|136|75|109|67|117|85|130|84|53|42|122|135|113|64|126|66|91|24|112|76|100|25|94|72|87|138|115|125|106|119|71|77|61|116|50|98|40|52|99|123|95|103|133|90|101|49|86|131|70|60|132|62|51|140|69|63|89|55|93|46|124|73|

I have a big problem :( btw, I had to change the stringinstr parameter to StringInStr($store,$ran&"|",0,1), eg: |115| it could find a 11 or a 15, or 5 or a 1...

Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites



Hi,

The other question about koda designer, is about that magnetic effect of tool's windows, when I create a styled tool windows, I won't get that effect (and yes, I set the parent window when created the tool window :) )

hm? I haven't tried the koda designer, maybe this :

#include <WindowsConstants.au3>

$GUI_Main = GUICreate("Parent")
GUISetState()

GUICreate("Child", 250, 250, -1, -1, Default, BitOR($WS_EX_TOOLWINDOW, $WS_EX_MDICHILD), $GUI_Main)
GUISetState()

While 1
    Sleep(1000)
WEnd

I had to change the stringinstr parameter to StringInStr($store,$ran&"|",0,1), eg: |115| it could find a 11 or a 15, or 5 or a 1...

That's why you need to check for every number you haved generated (I think), so here you go :

#include <Array.au3>

Dim $aGeneratedNumbers[1], $iUBound = 10

For $iGeneredNumber = 0 To $iUBound -1
    $iRdmNumber = Random(1, $iUBound, 1)

    If $iGeneredNumber > 0 Then
        While 1
            If _ArraySearch($aGeneratedNumbers, $iRdmNumber) = -1 Then ExitLoop

            $iRdmNumber = Random(1, $iUBound, 1)
        WEnd

        ReDim $aGeneratedNumbers[$iGeneredNumber +1]
        $aGeneratedNumbers[$iGeneredNumber] = $iRdmNumber
    Else
        $aGeneratedNumbers[0] = $iRdmNumber
    EndIf
Next

_ArrayDisplay($aGeneratedNumbers)

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 this post


Link to post
Share on other sites

Hi,

hm? I haven't tried the koda designer, maybe this :

#include <WindowsConstants.au3>

$GUI_Main = GUICreate("Parent")
GUISetState()

GUICreate("Child", 250, 250, -1, -1, Default, BitOR($WS_EX_TOOLWINDOW, $WS_EX_MDICHILD), $GUI_Main)
GUISetState()

While 1
Sleep(1000)
WEnd

Thats what I done, it doesn't have the window parent's edge magnetic effect :s

That's why you need to check for every number you haved generated (I think), so here you go :

#include <Array.au3>

Dim $aGeneratedNumbers[1], $iUBound = 10

For $iGeneredNumber = 0 To $iUBound -1
$iRdmNumber = Random(1, $iUBound, 1)

If $iGeneredNumber > 0 Then
While 1
If _ArraySearch($aGeneratedNumbers, $iRdmNumber) = -1 Then ExitLoop

$iRdmNumber = Random(1, $iUBound, 1)
WEnd

ReDim $aGeneratedNumbers[$iGeneredNumber +1]
$aGeneratedNumbers[$iGeneredNumber] = $iRdmNumber
Else
$aGeneratedNumbers[0] = $iRdmNumber
EndIf
Next

_ArrayDisplay($aGeneratedNumbers)

Br, Firefox.

ahah works fine, thanks, why you do ReDim $aGeneratedNumbers[$iGeneredNumber +1] in stead of Dim $aGeneratedNumbers[$iUBound] ? any difference?

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

ahah works fine, thanks, why you do ReDim $aGeneratedNumbers[$iGeneredNumber +1] in stead of Dim $aGeneratedNumbers[$iUBound] ? any difference?

Because you're not giving the array "room to breath" you need to re-dimension the array by the current size plus one.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Because you're not giving the array "room to breath" you need to re-dimension the array by the current size plus one.

? if I know the needed size, why shouldn't set it to that size? (I didn't get the expression "room to breath" lol, sorry english is not my native language as you could already notice ;) ) Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

? if I know the needed size, why shouldn't set it to that size? (I didn't get the expression "room to breath" lol, sorry english is not my native language as you could already notice ;) )

Oh I didn't notice that the variable inside the array for ReDim was the new size.

"Room to breath" just means giving something a little bit more space, so it can change/move/whatever. I should have said "Room to grow".

Share this post


Link to post
Share on other sites

Oh I didn't notice that the variable inside the array for ReDim was the new size.

"Room to breath" just means giving something a little bit more space, so it can change/move/whatever. I should have said "Room to grow".

yup is the needed size if all works until the end.

I thought you said "Room to breath" in the way of doesn't have arrest or high cpu consumtion...nevermind, got it ;) thanks


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

? if I know the needed size, why shouldn't set it to that size?

Then the _ArraySearch won't search where there is no generated numbers, or you can do :

#include <Array.au3>

Dim $iUBound = 10, $aGeneratedNumbers[$iUBound]

For $iGeneredNumber = 0 To $iUBound - 1
    $iRdmNumber = Random(1, $iUBound, 1)

    If $iGeneredNumber > 0 Then
        While 1
            If _ArraySearch($aGeneratedNumbers, $iRdmNumber, 0, $iGeneredNumber +1) = -1 Then ExitLoop

            $iRdmNumber = Random(1, $iUBound, 1)
        WEnd

        $aGeneratedNumbers[$iGeneredNumber] = $iRdmNumber
    Else
        $aGeneratedNumbers[0] = $iRdmNumber
    EndIf
Next

_ArrayDisplay($aGeneratedNumbers)

Br, FireFox.

Edited by 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 this post


Link to post
Share on other sites

Then the _ArraySearch won't search where there is no generated numbers, or you can do :

#include <Array.au3>

Dim $iUBound = 10, $aGeneratedNumbers[$iUBound]

For $iGeneredNumber = 0 To $iUBound - 1
$iRdmNumber = Random(1, $iUBound, 1)

If $iGeneredNumber > 0 Then
While 1
If _ArraySearch($aGeneratedNumbers, $iRdmNumber, 0, $iGeneredNumber +1) = -1 Then ExitLoop

$iRdmNumber = Random(1, $iUBound, 1)
WEnd

$aGeneratedNumbers[$iGeneredNumber] = $iRdmNumber
Else
$aGeneratedNumbers[0] = $iRdmNumber
EndIf
Next

_ArrayDisplay($aGeneratedNumbers)

Br, FireFox.

I didn't know that, if is empty _ArraySearch should skip it...

thanks for help/explanations


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

I thought you said "Room to breath" in the way of doesn't have arrest or high cpu consumtion...nevermind, got it ;) thanks

Another similar meaning phrase that you may run into, is 'elbow room'. Just to help you some maybe.. :graduated:
1 person likes this

- 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

Another similar meaning phrase that you may run into, is 'elbow room'. Just to help you some maybe.. :graduated:

now I'm learning english expression at a programming site :graduated:

just one las question, a variable set inside a function, will be deleted after function ends? (sparing RAM space)


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

just one las question, a variable set inside a function, will be deleted after function ends? (sparing RAM space)

Hi,

If this variable is set as Local, then yes because it will be only declared inside this function.

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 this post


Link to post
Share on other sites

Hi,

If this variable is set as Local, then yes because it will be only declared inside this function.

Br, FireFox.

Hi,

so i need to set as local, if I just define that variable, would it be local to? or its really needed to add local before setting the variable values?

sorry for asking


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

As long as you don't have the option set that you must declare all variables, then you don't NEED the local before it, but you should develop the practice of declaring your variables correctly. If you ever release a UDF where the variables aren't declared as local, then it could cause issues with someone using it because they might have declared the same variable name in their script as a Global.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

As long as you don't have the option set that you must declare all variables, then you don't NEED the local before it, but you should develop the practice of declaring your variables correctly. If you ever release a UDF where the variables aren't declared as local, then it could cause issues with someone using it because they might have declared the same variable name in their script as a Global.

alright then, thanks for the help ;)

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

You just want to scramble a sequence of numbers?

If you want to do it fast you'd avoid using any _Array() functions or calling Random() more than once per number output...

#include <Array.au3> ; test

; load test array
Dim $Array[141] = [140]
For $x = 1 to 140
    $Array[$x] = $x
Next
_ArrayDisplay($Array) ; test

; scramble it
For $i= $Array[0] to 2 Step -1
    $new = Random(1, $i, 1)
    $tmp = $Array[$i]
    $Array[$i] = $Array[$new]
    $Array[$new] = $tmp
Next
_ArrayDisplay($Array) ; test
1 person likes this

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