Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random numbers
#2
Hi there. There are a couple of reasons.

You can't put a SUB in between main code like that.  Move the SUB to the end of main code.  AI chatbots often make this mistake when making QB64 code, even after telling it not to do it.

Next thing, the SUB won't recognize the Dim variables you made in the main code unless they are DIM Shared, or if the are DIMmed inside the SUB instead.

I moved the DIMs to inside the SUB below and it works, you could DIM them as shared in the main code instead if you need to use them in other SUB's.

- Dav

Code: (Select All)

' Seed the random number generator
Randomize Timer

' Call the subroutine to generate and display the numbers
GenerateUniqueNumbers


Sub GenerateUniqueNumbers

    Dim numbers(5) As Integer
    Dim count As Integer
    Dim i As Integer
    Dim newNumber As Integer
    Dim isDuplicate As Integer

    count = 0
    Do
        ' Generate a random number between -1 and 71
        newNumber = Int(Rnd * 73) - 1
        isDuplicate = 0

        ' Check if the number is already in the array
        For i = 1 To count
            If numbers(i) = newNumber Then
                isDuplicate = 1
                Exit For
            End If
        Next i

        ' If it's not a duplicate, add it to the array
        If isDuplicate = 0 Then
            count = count + 1
            numbers(count) = newNumber
        End If
    Loop Until count = 5

    ' Print the selected numbers
    Print "The 5 unique numbers are:"
    For i = 1 To 5
        Print numbers(i)
    Next i
End Sub

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
random numbers - by badger - 11-06-2024, 11:15 PM
RE: random numbers - by Dav - 11-06-2024, 11:30 PM
RE: random numbers - by badger - 11-06-2024, 11:42 PM
RE: random numbers - by Dav - 11-06-2024, 11:51 PM
RE: random numbers - by Kernelpanic - 11-07-2024, 05:24 PM
RE: random numbers - by bplus - 11-07-2024, 05:52 PM
RE: random numbers - by bplus - 11-07-2024, 07:03 PM
RE: random numbers - by bplus - 11-07-2024, 07:10 PM
RE: random numbers - by DANILIN - 11-08-2024, 02:22 AM



Users browsing this thread: 2 Guest(s)