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 5986823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:43:39+00:00 2026-05-22T22:43:39+00:00

I have a table like follows: Potion { id, name, freq } I gave

  • 0

I have a table like follows:

Potion { id, name, freq }

I gave away potions to users in ‘random’ manner. but I want to be able to control frequency of the randomness because some potions is more powerful than others. What’s the best way to do this?

I am thinking of something like this:

  id | name  |   freq
  -------------------
   1 | light |     5
   2 | fire  |    10
   3 | water |    10
   4 | earth |    10
   5 | air   |     5

light and air are more powerful than fire, water, and earth so they have smaller frequencies.

sum = select sum(*) from potions;
x = random(1..sum)
table = select * from potions;
offset = 0
for each element in table
  if offset + element[freq] > x
     chosen = element[name]
  end
  offset += element[freq]
end

I think the code above (ruby + sql) will works but its not scalable and i very much doubt it that it’s the most efficient way of doing it. Could anyone please help me to do so?

Thank you in advance,

  • 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-22T22:43:40+00:00Added an answer on May 22, 2026 at 10:43 pm

    You first need to create a view on top of RAND() so it returns the same value for each invocation during execution (otherwise you’ll get weird results):

    create view constant_rand as select rand() as rand;
    

    Next select a random row weighted by the cumulative freq:

    select id
    from potion p1
    join constant_rand
    where rand * (select sum(freq) from potion) between
        -- sum of freq below this row
        ifnull((select sum(freq) from potion p2 where p2.id < p1.id), 0)
        -- sum of freq below and including this row
        and (select sum(freq) from potion p2 where p2.id <= p1.id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like as follows: SoftwareName Count Country Project 15 Canada Visio
I have table definition like below: Place (id, name) Review (id, userid, placeid) Favorite
I have a table like the following : code ---> INTEGER name ---> CHAR
I have a table like this: name code group john 12 smith 15 how
I have a table like this. ID Name Source ID 1 Orange 0 2
I have a table like follows: mysql> select * from tries; +----+--------+-----------+ | id
Assume that I have a table structure like seen below location|name ------------- NY|john London|neil
If I have a table as follows: ID | Name | Category | Parent
I have table as follows emp_id, emp_name, emp_address, emp_salary, emp_rating I would like to
I have table as follows emp_id, emp_name, emp_address, emp_salary, emp_rating I would like to

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.