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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:47:29+00:00 2026-06-14T22:47:29+00:00

I want to create a weighted random number generator in CoffeeScript. Here is the

  • 0

I want to create a weighted random number generator in CoffeeScript.

Here is the Javascript code:

// init
var chances = {
        red: 1,
        blue: 4,
        yellow: 10
    },
    bag = [];

// fill the bag with the values
for (var chance in chances) {
    for (var i=0; i<chances[chance]; ++i) {
        bag.push(chance);
    }
}

// get random element
var index   = Math.floor(Math.random()*bag.length,
    element = bag[index];

Of course I can create it in a not very elegant way (without the variable init):

for chance, value of chances
    for [1..value]
        bag.push(chance)

index   = Math.floor(Math.random()*bag.length;
element = bag[index];

I want to simplify the code, and create the best solution but I’m stucked:

bag = ((k for [1..v]) for k, v of chances)

This code create an Array with Arrays in it with the desired values, but not that I want obviously, and I don’t know how to do it in a nice way.

  • 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-06-14T22:47:31+00:00Added an answer on June 14, 2026 at 10:47 pm

    I think what you are looking for is something like:

    bag = [].concat.apply [], ((k for [1..v]) for k, v of chances)
    

    However, personally I do not think this path leads to simpler code at all (as you wrote you were trying to reach). Your first take with two for loops is simple and directly understandable by all programmers. I would stick with that.

    for chance, value in chances
        for [1..value]
            bag.push chance
    

    Also note that if you look at the Javascript code generated by these solutions the double for loop is a lot less complex.

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

Sidebar

Related Questions

i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
i want create XML file here is my following code String fileName = jasstech.xml;
i want create a counter that retrieve the number of month, day, hour, minute
I want create a new JavaScript object based on an existing JavaScript object. Till
I want create simple puzzle to select random 5 names from input. Lets say
I want create a plugin in jquery. this is my html code : <div
i want create multiple search where statement $where_search is a multiple condition from post
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I want create an application with animate button? how can i do? after click

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.