0

Im trying to generate a random ID number so when the user clicks a button it redirects to a random artist. I was able to do it and it works quite well with the code below.

var artists = _context.Artists;
var totalArtists = artists.Count();
Random randomArtistNumber = new Random();
int randomArtistID = randomArtistNumber.Next(1, totalArtists + 1);

if (button.Equals("btnArtist"))
{
    return RedirectToAction("Details", "ArtistModels", new { id = randomArtistID.ToString() });
}

The problem here is that if the user deletes an artist then im going to have an ID number that no longer exists in the random list created above. How would I go about creating a List<> of all ArtistID's in the Artist Table? It would be much better to just pick a random number from within a list of active ID's

ArtistModel below

public class ArtistModel
    {
        [Key]
        public int ArtistID { get; set; }
        public string ArtistName { get; set; }
        public string CountryOfOrigin { get; set; }
        public int SubgenreID { get; set; }
        public SubgenreModel Subgenre { get; set; }

        public ICollection<AlbumModel> Albums { get; set; }
    }
elementmg
  • 234
  • 2
  • 14
  • Does this answer your question? [EF Code First: How to get random rows](https://stackoverflow.com/questions/7781893/ef-code-first-how-to-get-random-rows) – Charlieface Jan 19 '21 at 01:38

2 Answers2

1

I'm guessing you are using Entity Framework?

You should be able to get a list of ids from your artists by doing something like this.

var artistIds = artists.Select(a => a.Id).ToList();

To do that correctly you'd need to ensure that you were always loading all artists from the context.

Might be better if you random function had a limit, like maybe randomly select from the first 200 artists or something.

GetFuzzy
  • 2,116
  • 3
  • 26
  • 42
  • This works great thank you, i was unaware of the Select function, Thats exactly what i was looking for. – elementmg Jan 19 '21 at 01:47
  • The problem with this approach is that if the number of artists is huge, then your list will be huge. Another way to look at it is by not caching the IDs. Instead, pull up a random artist. If the artist lookup fails, reach back into the well and pull up another one. Do that until you get a hit. – Flydog57 Jan 19 '21 at 01:51
  • Would a huge list just become a memory issue? – elementmg Jan 19 '21 at 05:03
  • @elementmg Yes, a large list could become a memory issue. It's not very practical for a number of reasons. As the list of artists grows it becomes more unsustainable. You could easily apply some logic where you first get the total number of records you have, then get 100 record ids from somewhere in your total range, then apply randomization to that... – GetFuzzy Jan 19 '21 at 14:20
1

Since you already have the artists, what you can actually do is get a random artist, it wouldnt be much different from what you have

Something like:

int randomArtistIndex = randomArtistNumber.Next(1, totalArtists); 
var artist = artists[randomArtistIndex]

and then

return RedirectToAction("Details", "ArtistModels", new { id = artist.ArtistID.ToString() });
nalnpir
  • 1,167
  • 6
  • 14