La Web del Programador: Comunidad de Programadores
 
    Pregunta:  64736 - PROBLEMA CON NUMEROS ALEATORIOS
Autor:  Gonzalo
Hola, estoy armando un programa en visual studio y necesito crear 4 numeros aleatorios diferentes entre 10
Buscando en internet encontre un algoritmo que me funciono a medias, lo estudie y lo modifique y me andubo bastante bien, pero cada tanto me da dos veces el numero 1 en distintas posiciones, y realmente no se en donde esta el problema, aqui les dejo el algoritmo a ver si alguien me puede ayudar a que no se repita. Aclaro, solamente me repite el numero 1, y de los 4 numeros, solamente se repite 2 veces ej: 3, 1, 1, 7, o 1, 1, 4, 6, etc...
Public Sub Naleatorios()

Randomize()

For i = 0 To 4

If i = 1 Then
numeros(j) = Int((10 * Rnd()) + 1)
If j = 4 Then
j = j
Else
j = j + 1
End If
Else
retorno:

For x = 0 To 3
temporal = Int((10 * Rnd()) + 1)

If temporal = numeros(1) Then
GoTo retorno
End If
If temporal = numeros(2) Then
GoTo retorno
End If

If temporal = numeros(3) Then
GoTo retorno
End If
If temporal = numeros(4) Then
GoTo retorno
End If

numeros(j) = temporal
If j = 4 Then
j = j
Else
j = j + 1
End If
Next x
End If
Next i

End Sub

Muchas gracias por su tiempo

  Respuesta:  Fernando Fumero
Option Explicit
Dim VECTOR(1 To 7) As Integer
Dim N As Integer

Private Sub Command1_Click()
Dim REPETIDO As Boolean
Dim NUMERO As Integer
Dim I As Integer
Randomize Timer
NUMERO = Int((10 * Rnd()) + 1)
For N = 1 To UBound(VECTOR())
Do
REPETIDO = False
NUMERO = Int((10 * Rnd()) + 1)
For I = 1 To N
If VECTOR(I) = NUMERO Then REPETIDO = True
Next I
If Not REPETIDO Then VECTOR(N) = NUMERO
Loop Until Not REPETIDO
Next N
End Sub

Private Sub Command2_Click()
Label1 = ""
For N = 1 To UBound(VECTOR())
Label1 = Label1 & VECTOR(N) & " | "
Next N
End Sub

Cortesía de FUMERO CORP. - [email protected] -