Results 1 to 3 of 3

Thread: [RESOLVED] Grasping a weighted random

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    PA
    Posts
    365

    Resolved [RESOLVED] Grasping a weighted random

    I have searched this forum, plus google and stackoverflow as well. I've seen examples, discussions, etc about the process. However, I'm still unable to grasp how it would be implemented in the design I have come up with.

    Background: I'm creating a music player with a custom rating for each song. Ratings are held in a sqlite database, all defaulted at 5. The ratings can go from 0 - 10. The whole idea is implemented when the random or shuffle toggle is on.

    It's easy for me to query all the songs with their ratings at once. I can return all these rows into a new ArrayList(), let's call it songratings if need be, all sorted according to rating, higher -> lower.

    The next part, I'm looking to pull a random number, that has more of a chance at the higher rated songs, but not a direct % must be played. Let's exclude 0 ratings from the arraylist, as 0 rated songs won't be played.

    So, if for example, I have songs rated 1-10, I'm most likely to hear the songs rated 8-10, decently likely to hear 6-7, middle is 5, less likely for 3-4, and least likely for 1-2.

    I'm trying to figure out something to this effect:
    Ratings: 10 - 14% chance
    9 - 14%
    8 - 13%
    7 - 12%
    6 - 11%
    5 - 10%
    4 - 8%
    3 - 7%
    2 - 6%
    1 - 5%

    I think I might have to pull a random to choose a percentage, then pull a random out of the ones with those ratings? Sorry for the long post, first time delving into random stuff...

    EDIT: Oops, forgot 1 major point -> ratings might not exist. For example, every song starts with a default rating of 5 and spans out from there. So, I need to sort of base it off the ratings range in the arraylist I think?
    Last edited by detlion1643; Feb 6th, 2012 at 10:02 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Grasping a weighted random

    Use a Random object to generate a random int from 1 to 100. If the number is less than 6 then return 1, if it's less than 12 then return 2, if it's less than 19 then return 3, ... otherwise return 10.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Grasping a weighted random

    Here's a relatively generic solution:
    Code:
    private Random rng = new Random();
    private int[] weightings = {5, 6, 7, 8, 10, 11, 12, 13, 14, 14};
    
    private int GetWeightedRandomNumber()
    {
        int max = weightings.Sum();
        int unweighted = rng.Next(0, max);
        int threshold = 0;
    
        for (int weighted = 0; weighted < weightings.Length; weighted++)
        {
            threshold += weightings[weighted];
    
            if (unweighted < threshold)
            {
                return weighted;
            }
        }
    
        return 0; // Never used. Required by compiler for completeness.
    }
    Note that that method will return a zero-based index, in this case 0 to 9, so you would need to add 1 to get your desired range of 1 to 10. This solution works no matter the number of values in the range and the weightings are not required to be percentages, just ratios.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width