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() 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] - |