Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Sequenza numeri random

  1. #1

    Sequenza numeri random

    Vorrei sapere come avere una sequenza di numeri random nella quale almeno una volta un numero deve apparire e dove non ci siano quindi ripetizioni dello stesso numero.

    Questo è il codice

    Randomize timer
    //crea la sequenza con il randomize

    For i = 0 To 5
    numcasuale = Int(Rnd * 5)
    //numecasuale è il numero compreso tra 0 e 5
    Next i

    vorrei che la variabile numcasuale mostrasse si un numero casuale compreso tra 0 e 5 ma vorrei che non ripetesse un numero già mostrato e che mostrasse almeno una volta ogni numero.

    La sequenza invece di essere:

    2 3 2 5 0 1

    dovrebbe essere

    2 3 0 1 4 5

    i numeri anche se hanno una sequenza casuale non vengono ripetuti più di una volta e sono presenti tutti..

    Qualcuno sa come creare la sequenza?

  2. #2
    E' difficile da dire, soprattutto perchè il random di Visual Basic è molto ripetitivo. Per risolvere il problema un metodo molto spartano sarebbe quello di controllare se il numero è già uscito salvando tutti quelli usciti in un Array e controllarli ad ogni ciclo 1 ad 1 ed in caso positivo (numero già uscito) ricalcolarlo, questo per TOT numero di volte finchè non esce un numero nuovo. Al momento mi viene in mente solo questo, poi vedi un pò tu...

    Ciao
    Cerco ombrello vecchio, nuovo, moderno o antidiluviano; purché protegga da una pioggia che vien giù come Dio la manda. Fate presto che ho l’acqua alla gola. (Noè)

    C# programming and other stuffs

  3. #3
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    Io mi ero fatto una funzione che faceva esattamente quello che ha detto chipdb, la puoi provare con un Button:
    codice:
    Private Sub Command1_Click()
    Const vMin = 0
    Const vMax = 5
    
    Dim Cont As Integer
    Dim Vett() As Integer
    Dim VettStr As String
    
    
    ReDim Vett(0)
    For Cont = 1 To 6
      Vett(UBound(Vett)) = NumeroNonPresente(Vett, vMin, vMax)
      VettStr = VettStr & " " & CStr(Vett(UBound(Vett)))
      ReDim Preserve Vett(UBound(Vett) + 1)
    Next Cont
    If UBound(Vett) > 0 Then
      ReDim Preserve Vett(UBound(Vett) - 1)
    End If
    
    MsgBox VettStr
    End Sub
    
    
    Private Function NumeroNonPresente(Vettore() As Integer, ByVal vMin As Integer, ByVal vMax As Integer) As Integer
    Dim Cont As Integer
    Dim Uguale As Boolean
    
    Do
      Uguale = False
      NumeroNonPresente = NumeroEstratto(vMin, vMax)
      For Cont = 0 To UBound(Vettore) - 1
        If NumeroNonPresente = Vettore(Cont) Then
          Uguale = True
          Exit For
        End If
      Next Cont
    Loop Until Uguale = False
    End Function
    
    Private Function NumeroEstratto(vMin As Integer, vMax As Integer) As Integer
    Randomize Timer
    NumeroEstratto = Int((vMax - vMin + 1) * Rnd + vMin)
    End Function
    Ciao.

  4. #4
    Originariamente inviato da Mabi
    Io mi ero fatto una funzione che faceva esattamente quello che ha detto chipdb, la puoi provare con un Button:
    codice:
    Private Sub Command1_Click()
    Const vMin = 0
    Const vMax = 5
    
    Dim Cont As Integer
    Dim Vett() As Integer
    Dim VettStr As String
    
    
    ReDim Vett(0)
    For Cont = 1 To 6
      Vett(UBound(Vett)) = NumeroNonPresente(Vett, vMin, vMax)
      VettStr = VettStr & " " & CStr(Vett(UBound(Vett)))
      ReDim Preserve Vett(UBound(Vett) + 1)
    Next Cont
    If UBound(Vett) > 0 Then
      ReDim Preserve Vett(UBound(Vett) - 1)
    End If
    
    MsgBox VettStr
    End Sub
    
    
    Private Function NumeroNonPresente(Vettore() As Integer, ByVal vMin As Integer, ByVal vMax As Integer) As Integer
    Dim Cont As Integer
    Dim Uguale As Boolean
    
    Do
      Uguale = False
      NumeroNonPresente = NumeroEstratto(vMin, vMax)
      For Cont = 0 To UBound(Vettore) - 1
        If NumeroNonPresente = Vettore(Cont) Then
          Uguale = True
          Exit For
        End If
      Next Cont
    Loop Until Uguale = False
    End Function
    
    Private Function NumeroEstratto(vMin As Integer, vMax As Integer) As Integer
    Randomize Timer
    NumeroEstratto = Int((vMax - vMin + 1) * Rnd + vMin)
    End Function
    Ciao.

    Grazie funziona alla perfezione.. l'ho adattato al mio programma.


  5. #5
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    Prego

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.