Visual Basic - Comparar Números Aleatoios

Life is soft - evento anual de software empresarial
 
Vista:

Comparar Números Aleatoios

Publicado por Daniela Estrella (1 intervención) el 18/02/2014 00:32:57
Hola. Tengo que hacer un pagrama en visual basic que consta de generar 10 números aleatorios del 1 al 10 que sean enteros y no se repitan. Una vez obtenidos estos números aleatorios tengo que comparar uno con uno, es decir, si me dio 1,6,2,7,5,3,4,9,8,10, el primer número siempre tendrá el valor de 1 y después comparo: si el 6 es mayor a 1, se le pone 1, y si no es mayor le pongo cero y eso guardarlo en una matriz bk.

Eso es hacerlo 100 veces y tengo que sacar la probabilidad condicional de que el valor en la matriz bk sea igual a 1 dado que el anterior me dio uno.

Tengo esto en VBA

Sub NoRepeatsTest()
Const SIZE_OF_LIST As Integer = 10
Const SIZE_OF_SIM As Integer = 2
Dim iResults() As Integer
Dim i As Integer
Dim iPick As Integer
Dim iLimit As Integer
Dim iTmp As Integer
Dim probabilidad As Double
Dim contador As Integer
Dim u, p As Integer
Dim condicional As Double

ActiveSheet.Cells.Clear

' Generate an array of available values

ReDim iResults(SIZE_OF_LIST)
For j = 1 To SIZE_OF_SIM


For i = 1 To SIZE_OF_LIST
iResults(i) = i
Next

' iResults() now contains ordered list of values
' Prove it by outputting to column A:

For i = 1 To SIZE_OF_LIST
Range("A1").Offset(i - 1, 0).Value = iResults(i)
Next

'''''''''' Now shuffle iResults() ''''''''''
' Seed random number generator with system time
Randomize

' Set limit 1 below maximum array index
iLimit = SIZE_OF_LIST - 1

For i = 1 To SIZE_OF_LIST - 1
' Pick a random element from index=1 to iLimit
iPick = Int((iLimit) * Rnd) + 1

' Swap this element with the one above iLimit
iTmp = iResults(iLimit + 1)
iResults(iLimit + 1) = iResults(iPick)
iResults(iPick) = iTmp

' Reduce limit by 1
iLimit = iLimit - 1
Next

' Output iResults() to column B
ReDim b(1 To SIZE_OF_LIST, 1 To SIZE_OF_SIM) As Double
contador = 0
u = 0
p = 0
For i = 1 To SIZE_OF_LIST
Range("B1").Offset(i - 1, 0).Value = iResults(i)
If iResults(i) > iResults(i - 1) Then
b(i, j) = 1
contador = contador + 1
Else
b(i, j) = 0
End If
Cells(i, j + 3).Value = b(i, j)
probabilidad = contador / SIZE_OF_LIST
Next

MsgBox (probabilidad)
Next j


For i = 1 To SIZE_OF_LIST

For j = 1 To (SIZE_OF_SIM - 1)
MsgBox (b(i, j))
If b(i, j) = b(i, (j + 1)) = 1 Then
u = 1
u = u + 1
Else
u = 0
End If
MsgBox (u)
If b(i, (j + 1)) = 1 Then
p = 1
p = p + 1
Else
p = 0
End If
MsgBox (p)
Next j
Next i

condicional = (u / SIZE_OF_LIST) / (p / SIZE_OF_LIST)
MsgBox (condicional)

End Sub
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder