Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random numbers
#1
Hello

can someone tell me why this little program will not work i dont really understand why.

badger

thanks in advance

Code: (Select All)
DECLARE SUB GenerateUniqueNumbers()

DIM numbers(5) AS INTEGER
DIM count AS INTEGER
DIM i AS INTEGER
DIM newNumber AS INTEGER
DIM isDuplicate AS INTEGER

SUB GenerateUniqueNumbers
    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

' Seed the random number generator
RANDOMIZE TIMER

' Call the subroutine to generate and display the numbers
GenerateUniqueNumbers
Reply
#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
#3
Hello

thanks very much all that was a really big help. Have not used qb64 in a while.

Badger
Reply
#4
You are very welcome.  And welcome to the forum!

I forgot to mention this - You will notice I didn't put the DECLARE SUB in the corrected code I posted, but it still works.  In QB64PE you don't have to DECLARE SUB/FUNCTION before using them.  

- Dav.

Find my programs here in Dav's QB64 Corner
Reply
#5
The entire program basically consists of just one procedur, which only makes sense for an AI.  Rolleyes

Without a procedur, and with a query about how many numbers should be drawn.

Code: (Select All)

'Zufallsgenerator - 7. Nov. 2024

Option _Explicit

' Seed the random number generator
Randomize Timer

Dim As Integer anzahlNummern
Dim As Integer count
Dim As Integer newNumber
Dim As Integer isDuplicate

Print
Input "Anzahl der Zufallsnummern (Max 10): ", anzahlNummern
If anzahlNummern > 10 Then
  Beep: Print "Maximal 10 Nummern!"
  Sleep 2
  System
End If

Dim As Integer numbers(anzahlNummern)

count = 0
Dim As Integer i
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 = anzahlNummern

' Print the selected numbers
Print Using "The ## unique numbers are:"; anzahlNummern
For i = 1 To anzahlNummern
  Print numbers(i)
Next i

End
Reply
#6
Could just put numbers into a deck and shuffle deck and pull the numbers out like dealing cards. Garantee no duplicates and no having to worry about them!
b = b + ...
Reply
#7
Followup with code for most efficient shuffle known to mathematicians!:

Code: (Select All)
TopN = 52
ReDim n(1 To TopN) 'repeatable for ref
For i = 1 To TopN
    n(i) = i
Next

' shuffle
For i = TopN To 2 Step -1 ' Fisher Yates Shuffle of N Items
    Swap n(i), n(Int(Rnd * (i) + 1))
Next

' results of shuffle
For i = 1 To TopN
    Print "  "; i; "-"; n(i); Chr$(9);
Next
Print
b = b + ...
Reply
#8
Here is that code applied to your, @badger specific case:
Code: (Select All)
' Generate a random number between -1 and 71

' 0 to 71 = 72 cards plus 1 for -1 = 73 cards

' without repetition

Randomize Timer ' for new random set nearly every time
TopN = 73
ReDim n(1 To TopN) 'repeatable for ref
For i = 1 To TopN
    n(i) = i
Next

' shuffle
For i = TopN To 2 Step -1 ' Fisher Yates Shuffle of N Items
    Swap n(i), n(Int(Rnd * (i) + 1))
Next

' results of shuffle
For i = 1 To TopN
    Print "  "; i; "-"; n(i) - 2; Chr$(9); ' <<<<< subtract 2 for number -1 to 71
Next
Print

   

Ha! You have 14 sets of 5 unique and non intersecting sets of numbers from 1 Shuffle of deck of 73 cards.
b = b + ...
Reply
#9
Binary Sequence Predictor "game"?  (Read 10252 times)

https://qb64forum.alephc.xyz/index.php?t...#msg140722

My favorite circle string "1 goto 1"

  1 m = INT(RND*a)+1: IF ABS(d(i)-d(m)) < .1*a THEN v = v+1: GOTO 1

Controlled what all numbers is on new places

plus minus diapazon from original place

+ Shuffling Letters
https://qb64forum.alephc.xyz/index.php?topic=3982

Program above outputs repeats and the numbers -1 and 0
But even excluding -1 and 0, do we repetitions? There are

Code: (Select All)
For i = 1 To TopN ' results of shuffle
    Print "  "; i;
    If i = n(i) Then Print "=!!!";: rep = rep + 1 Else Print "-";:
    Print n(i) - 0; Chr$(9);
Next: Print: Print rep
End

So it's always worse than I have 0 repetitions
Write name of program in 1st line to copy & paste & save filename.bas
Insert program pictures: press print-screen-shot button
Open paint & Paste & Save as PNG
Add picture file to program topic

Russia looks world from future. Big data is peace data.
I never recommend anything & always write only about myself
Reply




Users browsing this thread: 2 Guest(s)