Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Random number

  1. #1
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Blog Entries
    1
    Rep Power
    154

    Random number

    I am looking for a fast way to produce a sequence of random numbers that dont repeat and in a specified range.

    ie. 1,4,6,9,2... you get the idea

    I will post what I am doing later tonight when I get home, I would be interested to see different ways of doing it.

    thanks

    Mike
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,828
    Blog Entries
    3
    Rep Power
    696

    Re: Random number

    Hi Mike,

    this is very dumb version, not very optimized ... but works


    Petr

    [code=thinbasic]

    dim Numbers(1) as long

    if Generate( Numbers, 10, 1, 10 ) = %TRUE then
    msgbox 0, "Generated:"+$CRLF+join$(Numbers, ",")
    else
    msgbox 0, "Error ocurred"
    endif

    function Generate( byref arr() as long, xNumbers as long, nFrom as long, nTo as long )

    local i as long
    local f as long

    ' -- Make sure the array is properly dimensioned
    if ubound(arr) <> xNumbers then
    redim arr(xNumbers)
    endIf

    ' -- Check if it is possible to generate such a series
    if xNumbers > nTo-nFrom+1 then
    msgbox 0, "Nice try, but impossible "
    Return %FALSE
    endIf

    ' -- Fill array with the numbers
    for i = 1 to xNumbers

    arr(i) = rnd(nFrom, nTo)

    if i > 1 then ' -- Check if value was already used
    f = 1
    do
    f = array scan arr(1) for i-1, =arr(i)
    if f = 0 then exit do
    arr(i) = rnd(nFrom, nTo)
    loop
    endIf

    next
    Return %True

    end function
    [/code]
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    62
    Posts
    3,869
    Rep Power
    398

    Re: Random number

    Here is my version Mike.
    Used logic from this site:
    http://www.techuser.net/randpermgen.html
    [code=thinbasic]Uses "Console"

    Dim I, N, R As Long
    N = 10
    Dim Ar(N) As Long
    '--- fill and array in order
    For I = 1 To N
    Ar(I) = I
    Print Str$( Ar(I) ) + $Crlf
    Next
    Print $Crlf + $Crlf
    '--- then rearrange that order randomly
    For I = 1 To N
    R = Rnd(I,N)
    Swap Ar(I),Ar(R)
    Print Str$( Ar(I) ) + $Crlf
    Next
    Console_Writeline($Crlf + "Press Any Key To End.")
    Console_Waitkey[/code]
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,828
    Blog Entries
    3
    Rep Power
    696

    Re: Random number

    Nice one Kent!,

    that is really smart, and completely eliminates check like "did I generated this already?"

    Little mod of your code, to handle situation like "I want 3 numbers from range 1 to 10":
    [code=thinbasic]

    Dim Numbers(1) as long

    Generate(Numbers, 3, 1, 10)
    msgbox 0, "Generated:"+Join$(Numbers, ",")


    sub Generate( byref arr() as long, xNumbers as long, nFrom as long, nTo as long )
    local i, r as long
    Local iMax as long = nTo-nFrom+1

    redim arr(iMax)

    ' -- Fill in all numbers
    for i = nFrom to nTo
    arr(i-nFrom+1) = i
    next

    ' -- Rearrange
    For i = 1 To iMax-1
    r = Rnd(i,iMax)
    Swap arr(i),arr(r)
    Next

    ' -- In case we need less numbers than range
    if iMax > xNumbers then reDim Preserve arr(xNumbers)

    end sub
    [/code]
    This code can be 10x faster than my original version, only in rare cases it is slower.


    Good work!,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  5. #5
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    62
    Posts
    3,869
    Rep Power
    398

    Re: Random number

    Good addition Petr! You are always thinking, thanks for sharing!!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,828
    Blog Entries
    3
    Rep Power
    696

    Re: Random number

    I am sure it could be boosted up by replacing:
    [code=thinbasic]
    for i = nFrom to nTo
    arr(i-nFrom+1) = i
    next
    [/code]

    ... with O2 or Asmosphere


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  7. #7

    Re: Random number

    Kent:

    I was unable to get your version running because under the copy of thinBasic 1.6.0.8 I am running, the"PRINT" alias for "Console_WriteLine" does not seem to be working. After I replaced the PRINT statements in your code with Console_WriteLine statements it worked fine. What version of thinBasic are you using?

    Don
    XPS 1710

  8. #8
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    62
    Posts
    3,869
    Rep Power
    398

    Re: Random number

    Hmmmm, I don't know why as I have the same version. Maybe Eros or Roberto can figure it out.

    I am using the latest preview version if that could be it?
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	version.PNG 
Views:	109 
Size:	8.8 KB 
ID:	4678  
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  9. #9
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,828
    Blog Entries
    3
    Rep Power
    696

    Re: Random number

    Hi GSAC3,

    PRINT is there since version 1.6.0.6... so that is odd.
    Try to put following:
    #minversion 1.6.0.8
    on the first line of the script. If it passes, it might be because of different Win98/NT version of Console module, but that is wild guess.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  10. #10
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Blog Entries
    1
    Rep Power
    154

    Re: Random number

    I have seperated the section of code I was talking about.

    Here are mine and Kent/Petr version for comparision.

    [code=thinbasic]%Numcount = 10
    DIM sMsg as STRING
    DIM N as WOrd VALUE = 1
    DIM l(%Numcount) as LONG
    DIM m(%NumCount) as LONG
    DIm p,q as LONG VALUE = 1
    DIM t1,t2,t3,t4 AS QUAD
    dim Numbers(%NumCount) as long

    '---Initialize hi resolution timer
    RANDOMIZE ' reseed the radomiser

    HiResTimer_Init
    T1 = HiResTimer_Get
    MyVersion()
    T2 = HiResTimer_Get

    sMsg = "Generated:"+$CRLF+join$(m, ",")+$CRLF
    sMsg += "Elapsed time in microseconds: " & FORMAT$(T2-T1, "#0")
    MSGBOX 0, sMSG

    t3 = HIRESTIMER_GET
    Generate '( Numbers, 10, 1, 10 )
    t4 = HIRESTIMER_GET
    sMsg = "Generated:"+$CRLF+join$(Numbers, ",")+$CRLF
    sMsg += "Elapsed time in microseconds: " & FORMAT$(T4-T3, "#0")
    MSGBOX 0, sMSG

    stop
    SUB MyVersion()
    For n = 1 to (%Numcount) : l(n) = n : NEXT
    DO
    p = RND(1,%Numcount) ' Generate a random number
    For n = 1 to %Numcount
    if l(p) = 0 THEN EXIT FOR ' if the random number has been used generate a new number
    m(q) = p : l(p) = 0 ' Store the random number and clear index
    q += 1 ' Increase the counter
    NEXT
    LOOP UNTIL q => %Numcount+1
    END SUB

    sub Generate() '( byref arr() as long, xNumbers as long, nFrom as long, nTo as long )
    local i, r as long
    'Local iMax as long = nTo-nFrom+1


    ' -- Fill in all numbers
    for i = 1 to %NumCount : Numbers(i) = i : next

    ' -- Rearrange
    For i = 1 To %NumCount
    r = Rnd(i,%NumCount)
    Swap Numbers(i),Numbers(r)
    Next

    ' -- In case we need less numbers than range
    'if iMax > xNumbers then reDim Preserve arr(xNumbers)

    end sub[/code]
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

Page 1 of 2 12 LastLast

Similar Threads

  1. Random Number
    By OneHitWonder in forum thinBasic General
    Replies: 1
    Last Post: 01-07-2010, 23:45

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •