Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:13+00:00 2026-05-13T08:07:13+00:00

Consider the following table: Song —- SongID GUID Primary Key Name Varchar dateAdded DateTime

  • 0

Consider the following table:

Song
----
SongID GUID Primary Key
Name Varchar
dateAdded DateTime

I am creating a website that plays songs from the Song table. I want to generate a randomly ordered playlist for each visitor of the site that persists across requests without using any server-side storage (no session-style or database storage.) The player has Play, Next Song, and Previous Song buttons. What is the most efficient way to persist this data across requests? What is the most efficient query for this information? I have a solution (in mySql) that works, but I am looking to see if something more efficient works. Also, I don’t want to bias the answers into using my solution. Please include a query in your answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:07:13+00:00Added an answer on May 13, 2026 at 8:07 am

    Update: Here’s the requirements that I followed, spelled out:

    • No matter how many songs there are in the table or the current location in a playlist, the information the client is required to store (e.g. in a cookie) must have a constant size.
    • Using ‘next’ 100 times followed by ‘prev’ 100 times should result in some sequence of 100 songs (possibly including duplicates) followed by that exact sequence reversed, regardless of any insertions that may have been made inbetween any ‘next’ or ‘prev’ action. (“Previous song always means the previous song.”)
    • A song that would have been in a playlist “when it was generated/initialized” and has since been deleted will be skipped.
      • Would not be hard to change my answer below to return a not-found indicator instead.

    I think you need one more piece of information: a secondary song key of an integer position, in addition to your GUID (or you could replace it with this). Then, combined with a PRNG, you can do the most simple and quick lookup possible. Pseudocode:

    def next_song(initial_seed, current_prng_index, high_song_index):
      """Returns the next song and the parameters to be later passed
      to next/prev_song."""
      while True:
        current_prng_index += 1
        current_seed = PRNG_advance(initial_seed, current_prng_index)
        song_index = song_index_from_seed(current_seed, high_song_index)
        song_id = (SELECT SongID FROM Songs WHERE SongIndex=song_index)
        if song_id: # test, somehow, for non-deleted songs
          return song_id, (initial_seed, current_prng_index, high_song_index)
          # include values that must be passed on the next call
    # prev is the same, except uses -= 1
    
    def song_index_from_seed(seed, highest):
      return seed % (highest + 1)
      # simple, but better possibilities might exist for your data
    
    def new_playlist():
      """Returns the parameters to be passed to next/prev_song."""
      high_song_index = (SELECT MAX(SongIndex) FROM Songs)
      return get_a_random_number(), 0, high_song_index
    

    This will get progressively slower on each next song and doesn’t allow wrapping by going backwards (without some creativity). If you find a suitable reversible PRNG (though uncommon among good PRNGs, AFAIK):

    def next_song(current_seed, high_song_index):
      while True:
        current_seed = PRNG_next(current_seed)
        song_index = song_index_from_seed(current_seed, high_song_index)
        song_id = (SELECT SongID FROM Songs WHERE SongIndex=song_index)
        if song_id: # test, somehow, for non-deleted songs
          return song_id, (current_seed, high_song_index)
          # include values that must be passed on the next call
    # prev is the same, except uses PRNG_prev
    

    This handles deletions by skipping those songs (or if you never delete that’s not even an issue), but otherwise doesn’t change the order no matter how many are deleted. Insertions are handled by the song_index_from_seed function, by constraining the index so new songs are never ‘seen’. This is also an infinite loop if all the available songs have been deleted, but I think this code handles all other corner cases.

    (Refactoring the next/prev versions to be simple wrappers around a common function isn’t hard, but left out here to increase clarity.)

    I’ve effectively replaced your dateAdded with my position index, but this is an improvement, since you don’t need to keep deleted rows as dummies (but still cannot reuse their index) as you would if the position index was computed by ordering on dateAdded.

    Using a PRNG like this seems naive, but works; and you’ll have to be careful that the chosen PRNG and song_index_from_seed combination behaves as you expect: it’s possible that some initial seeds appear to generate “non-random” playlists. (Not because they’re not random, but because listeners expect a mix of songs instead of just 4, 4, 9, 9, …)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following design: Company TABLE ( CompanyId int NOT NULL IDENTITY PRIMARY KEY,
Consider the following table named Persons : Key Name Type Date Pack 1 Pocoyo
Consider the following table which has the fields - id (int) and date_created (datetime):
Consider the following table: +-------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key |
Consider the following tables: Base, Primary key: Id Extension, Primary key: Id The primary
Consider the following table: create table temp ( name int, a int, b int
consider the following table create table sample(id, name, numeric, qno, ans1,ans2,ans3) sample data 1,
Please Consider the following table : As you can see the Name column has
Consider the following table : id | Name | City 1 | Roger |
Consider the following HTML table: <table id=myTable1> <tr id=TR1> <td><input type=text id=quantity1 name=quantity1 /></td>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.