Announcement

Collapse
No announcement yet.

Problem with Arrays and Random

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Arrays and Random

    I'm working on code that should select randomly from a list of 20 stored videos (vidNum is the number video picked, and vidMax is the max number of possible videos), play that video, then select another random video from the list *that has not been played yet*. The way I'm trying to do this is: setup an array[20], set all status flags to 0 at the start (not played), pick randomly from the list, if the selected video has already been played, keep picking until you get one that hasn't been played. When all have been played, reset the array and start over.

    It's not working. It's just playing the same video over and over, which is the exact opposite of what it's supposed to do. here's the relevant code:

    func resetPlayed() // Reset the "played" status for all videos to 0
    played[20] := [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    endfunc

    func pickRandom() // Picks a random video but only plays each once until all in the sequence have been played
    exitNow := 0;

    while(exitNow := 0)
    vidNum := ABS(RAND() % (vidMax-1));
    vidNum := vidNum + 1;
    if(vidNum >= (vidMax - 1))
    vidNum := 0;
    endif
    if(played[vidNum] := 0) // This one hasn't ben played yet
    exitNow := 1;
    endif
    wend

    played[vidNum] := 1;
    endfunc

    In theory this looks to me like it should work-- any idea what's wrong here? FYI there is some funky code with the "vidMax-1" section-- long story but this is required to pick the correct video (working fine in another app). I don't understand why the loop isn't working.

    Also, on a related note, I'm very frustrated with the GOLDELOX's inability to create true random numbers. Is there anything that can be done here? I start the code with a hard-coded 4-digit seed, but every time the board powers up, it picks the same numbers. Any help in this area would also be much appreciated.

    Thank you!

  • #2
    Hello,

    Indeed, as you already knew, RAND() function is not truly random. The same pattern
    of numbers will show up when you reset your display.

    For suggestions on how to generate random numbers, please check these threads:
    Need a random number generator fix. [post #3]
    Random is not Random? [post #4]

    For your convenience, here is a list of the suggestions from the threads:
    1. You could wire up a zener diode, use analog read and use the noise as value
    of your SEED().
    2. You could wire up an analogue input to pick up 'noise' and use it as your SEED value.

    Best regards,
    Sherwin

    Comment


    • robbok
      robbok commented
      Editing a comment
      Great ideas, thanks Sherwin for sharing!

  • #3
    Hi Rob,

    No worries, you're always welcome.

    If you have other inquiries, just let us know.

    Good luck and best regards,
    Sherwin

    Comment


    • #4
      Hi all, just to let everyone know that I solved my array problem. Turns out it was one of the most obvious programmer mistakes: using := inside a WHILE/IF statement instead of ==. Here's the incorrect statement:

      while(exitNow := 0)

      Here's the correct statement:

      while(exitNow == 0)

      I made the mistake twice (also in the if(played[vidNum] := 0) statement). Corrected both, and the randomization of the array is working like a charm.

      I also implemented the "write seed to SD card" routine-- also working great.

      Thanks again!

      Comment


      • #5
        Hi Rob,

        Indeed, ":=" initializes a value but "==" operator checks for the equality of two variables. It's great
        to hear you've figured it out on your own.

        Glad to know that you're no longer having a problem with randomization as well.

        For more information regarding the 4DGL, please have a look at the 4DGL Programmer's Reference
        Manual
        .

        Best regards,
        Sherwin

        Comment

        Working...
        X