Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

[Excel] Da se ne ponavljaju slucajni brojevi

[es] :: Office :: Excel :: [Excel] Da se ne ponavljaju slucajni brojevi

[ Pregleda: 4318 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

mirko1949
Miroslav Magdić
Kragujevac

Član broj: 87134
Poruke: 216
*.dynamic.sbb.rs.



+2 Profil

icon [Excel] Da se ne ponavljaju slucajni brojevi17.06.2008. u 10:28 - pre 193 meseci
U radnom listu u celijama A1 do A7 generisem slucajne brojeve 1-39 pomocu formule:
=TRUNC(RAND()*(39-1)+1)
Znaci cele brojeve od 1-39. Moze li se izbeci mogucnost ponavljanja istih brojeva u 2 celije, sto se ne bas retko desava.
 
Odgovor na temu

Brz_na_tastaturi
Svilajnac

Član broj: 63852
Poruke: 318
77.46.218.*



+1 Profil

icon Re: [Excel] Da se ne ponavljaju slucajni brojevi17.06.2008. u 15:58 - pre 193 meseci
Korišćem samo funkcija teško je izvodljivo.
Mnogo bolja varijanta je preko vba koda gde treba da definišeš sedam promenjlivih (ili jedan niz od sedam elemenata) koji ce da dobijaju random vrednosti s tim da kontrolišeš da svaka nova vrednost nije ista sa nekom od prethodnih, a zatim te promenljive upiseš u ćelije.
Možda je tačno da je sve već rečeno, samo nisu svi sve čuli i razumeli ...
 
Odgovor na temu

mirko1949
Miroslav Magdić
Kragujevac

Član broj: 87134
Poruke: 216
*.dynamic.sbb.rs.



+2 Profil

icon Re: [Excel] Da se ne ponavljaju slucajni brojevi17.06.2008. u 16:36 - pre 193 meseci
Eh, lako je to reci ali tesko uciniti. Nesto sam pokusavao ali mi neide.
 
Odgovor na temu

timmy
Jovan Timotijevic

Moderator
Član broj: 37087
Poruke: 634

Sajt: www.e-tim.net


+89 Profil

icon Re: [Excel] Da se ne ponavljaju slucajni brojevi17.06.2008. u 19:23 - pre 193 meseci
Probaj Mirko sa ovim. Verovatno je moglo i optimalnije, ali ovo je na brzinu:

Code:

Private Sub LotoBrojevi()
Dim i(7) As Byte
Dim k As Byte
Dim j As Byte
Dim br As Byte
    For j = 1 To 7
        i(j) = 0
    Next j
    i(1) = Int(Rnd() * 39) + 1
    Range("A1") = i(1)
    For j = 2 To 7
        For k = 1 To j - 1
            Do
                br = Int(Rnd() * 39) + 1
            Loop While i(k) = br
        Next k
        i(j) = br
        Range("A" & j) = br
    Next j
End Sub


Pozdrav
 
Odgovor na temu

Brz_na_tastaturi
Svilajnac

Član broj: 63852
Poruke: 318
79.101.192.*



+1 Profil

icon Re: [Excel] Da se ne ponavljaju slucajni brojevi17.06.2008. u 21:51 - pre 193 meseci
Evo ti još jedna verzija, ako ti ne treba ovaj deo za sortiranje izbaci ga.

Code:

Sub Brojevi7od39()

Dim i As Integer
Dim k As Integer
Dim broj(7) As Integer
Dim pomocniBroj As Integer 'Potreban ako hoces da sortiras dobijene brojeve


For i = 1 To 7

PONOVI:
    
    broj(i) = Int((39 * Rnd) + 1)
    For k = 1 To i - 1
        If broj(k) = broj(i) Then
            GoTo PONOVI
        End If
    Next k
    
Next i
'**** Deo koda za sortiranje ako je i to potrebno
For i = 1 To 7
    For k = 1 To i - 1
        If broj(k) > broj(i) Then
            pomocniBroj = broj(k)
            broj(k) = broj(i)
            broj(i) = pomocniBroj
        End If
    Next k
Next i
'**************************************************

For i = 1 To 7
    Cells(1, i) = broj(i)
Next i

End Sub

Možda je tačno da je sve već rečeno, samo nisu svi sve čuli i razumeli ...
 
Odgovor na temu

mirko1949
Miroslav Magdić
Kragujevac

Član broj: 87134
Poruke: 216
*.zastava.net.



+2 Profil

icon Re: [Excel] Da se ne ponavljaju slucajni brojevi18.06.2008. u 10:38 - pre 193 meseci
Hvala Brzi i hvala timmy. Oba koda odlicni funkcionisu.
 
Odgovor na temu

3okc
Χoᴘᴦoѡ

Moderator
Član broj: 811
Poruke: 1319

Jabber: 3okc@elitesecurity.org


+116 Profil

icon Re: [Excel] Da se ne ponavljaju slucajni brojevi18.06.2008. u 11:32 - pre 192 meseci
Vrlo detaljno se temom bavio C. Pearson, vidi: Random Numbers In Excel

Pogledaj deo Getting An Array Of Unique, Non-Duplicated Values, imaš gotovu f-ju sa opisom kako radi i prikačen fajl sa primerima..

Preneću samo f-ju (iako opširno iskomentarisanu):
Code:

Public Function UniqueRandomLongs(Minimum As Long, Maximum As Long, _
            Number As Long, Optional ArrayBase As Long = 1, _
            Optional Dummy As Variant) As Variant
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' UniqueRandomLongs
' This returns an array containing elements whose values are between the Minimum and
' Maximum parameters. The number of elements in the result array is specified by the
' Number parameter. For example, you can request an array of 20 Longs between 500 and
' 1000 (inclusive).
' There will be no duplicate values in the result array.
'
' The ArrayBase parameter is used to specify the LBound of the ResultArray. If this
' is omitted, ResultArray is 1-based.
'
' The Dummy argument is to be used only when the function is called from a worksheet.
' Its purpose is to allow you to use the NOW() function as the Dummy parameter to force
' Excel to calculate this function any time a calculation is performed. E.g.,
'       =UniqueRandomLongs(100,199,10,NOW())
' If you don't want to recalulate this function on every calculation, omit the Dummy
' parameter. The Dummy argument serves no other purpose and is not used anywhere
' in the code.
'
' The function returns an array of Longs if successful or NULL if an error occurred
' (invalid input parameter).
'
' Note: The procedure creates its own array of size (Maximum-Minium+1), so very large
' differences between Minimum and Maximum may cause performace issues.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim SourceArr() As Long
Dim ResultArr() As Long
Dim SourceNdx As Long
Dim ResultNdx As Long
Dim TopNdx As Long
Dim Temp As Long

''''''''''''''''''''''''''''''''''''''
' Test the input parameters to ensure
' they are valid.
''''''''''''''''''''''''''''''''''''''
If Minimum > Maximum Then
    UniqueRandomLongs = Null
    Exit Function
End If
If Number > (Maximum - Minimum + 1) Then
    UniqueRandomLongs = Null
    Exit Function
End If
If Number <= 0 Then
    UniqueRandomLongs = Null
    Exit Function
End If

Randomize
''''''''''''''''''''''''''''''''''''''''''''''
' Redim the arrays.
' SourceArr will be sized with an LBound of
' Minimum and a UBound of Maximum, and will
' contain the integers between Minimum and
' Maximum (inclusive). ResultArray gets
' a LBound of ArrayBase and a UBound of
' (ArrayBase+Number-1)
''''''''''''''''''''''''''''''''''''''''''''''
ReDim SourceArr(Minimum To Maximum)
ReDim ResultArr(ArrayBase To (ArrayBase + Number - 1))
''''''''''''''''''''''''''''''''''''''''''''
' Load SourceArr with the integers between
' Minimum and Maximum (inclusive).
''''''''''''''''''''''''''''''''''''''''''''
For SourceNdx = Minimum To Maximum
    SourceArr(SourceNdx) = SourceNdx
Next SourceNdx

''''''''''''''''''''''''''''''''''''''''''''''
' TopNdx is the upper limit of the SourceArr
' from which the Longs will be selected. It
' is initialized to UBound(SourceArr), and
' decremented in each iteration of the loop.
' Selections from SourceArr are always in the
' region including and to the left of TopNdx.
' The region above (to the right of) TopNdx
' is where the used numbers are stored and
' no selection is made from that region of
' the array.
''''''''''''''''''''''''''''''''''''''''''''''
TopNdx = UBound(SourceArr)
For ResultNdx = LBound(ResultArr) To UBound(ResultArr)
    ''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Set SourceNdx to a random number between 1 and
    ' TopNdx. ResultArr(ResultNdx) will get its value from
    ' SourceArr(SourceNdx). Only elements of SourceArr
    ' in the region of the array below (to the left of)
    ' TopNdx (inclusive) will be selected for inclusion
    ' in ResultArr. This ensures that the elements in
    ' ResultArr are not duplicated.
    ''''''''''''''''''''''''''''''''''''''''''''''''''
    SourceNdx = Int((TopNdx - Minimum + 1) * Rnd + Minimum)
    ResultArr(ResultNdx) = SourceArr(SourceNdx)
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Now, swap elements SourceNdx and TopNdx of SourceArr,
    ' moving the value in SourceArr(SourceNdx) to the region
    ' of SourceArr that is above TopNdx.  Since only elements
    ' of SourceArr in the region below TopNdx (inclusive) are
    ' possible candidates for inclusion in ResultArr, used
    ' values are placed at TopNdx to ensure no duplicates.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Temp = SourceArr(SourceNdx)
    SourceArr(SourceNdx) = SourceArr(TopNdx)
    SourceArr(TopNdx) = Temp
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Decrment TopNdx. This moves the effective UBound of SourceArr
    ' downwards (to the left), thus removing used numbers from the
    ' possibility of inclusion in ResultArr. This ensures we have
    ' no duplicates in the ResultArr.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    TopNdx = TopNdx - 1
Next ResultNdx

''''''''''''''''''''''''''''''
' Return the result array.
''''''''''''''''''''''''''''''
UniqueRandomLongs = ResultArr

End Function

Have you found helpful examples, solutions or ideas-in-work, please consider the
following:
• Citation of *actual page* you are going to using is welcome.
• Have you saw "Value Added" examples you are going to incorporate to your
profitable blog, daily job or any money making venture: How about online donation?
↗Ana & Vlade Divac Foundation
Hvala!
 
Odgovor na temu

[es] :: Office :: Excel :: [Excel] Da se ne ponavljaju slucajni brojevi

[ Pregleda: 4318 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.