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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:21:14+00:00 2026-06-03T23:21:14+00:00

I am doing a small app that involves the system randomly generating objects. Here’s

  • 0

I am doing a small app that involves the system randomly generating objects. Here’s how it goes

Each objects has a field that specifies the “type” of the object. Let’s say fire, ice, wind (not the actual thing in the app though)
Each type has a field called “possibilities” that determines the possibilities of that type being generated.
The default value for this field is of course 1/numberOfTypes
Another field of a type is its “order” in the list, let’s say fire is 1st and ice is 2nd and wind is 3rd.

My naive solution to this is to use
Math.floor(Math.random()*100 / defualtPossibilities) and match it with type.oder
So if the random value is .75, and there are three types, Math.floor(...) will return 2 which means that the object is an ice object.

However, I meet with some potential problems:

  1. It is possible to alter the possibilities value of each type. In this case I can still generate a random number and use a loop to check each type.order*type.possibilities until it exceeds the random number. But I’m not sure whether there’s a better way of doing it.

  2. When you change one type.possibilities, the other type. possibilities must change in accordance to keep the total possibilities 100. I plan to do this by divide evenly the amount of change to all the other types. Is it a good approach.

I’m sorry if I’m vague at some points but please help me ^-^

Thank you all!

  • 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-03T23:21:16+00:00Added an answer on June 3, 2026 at 11:21 pm

    Here’s a short solution :

     float weights[]; // each >0
    
     public int nextIndex() {     
         float sum =0;
         for (int i=weights.length; i-->0;) sum+=weights[i];
         float r = myrandom.nextFloat()*sum;
         sum = 0;
         for (int i=0; i<weights.length-1; i++) {
             sum += weights[i];
             if (r<sum) return i;
         }
         return weights.length-1;
     }
    

    You can change each weight independently. The idea is simply to recompute the sum of the weights instead of supposing it’s 100.

    Obviously, for the best performances you could precompute the sum.

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

Sidebar

Related Questions

I'm doing a small multi-threaded app that uses asynchronous TCP sockets, but I will
I'm building a small app that takes things entered into an input field and
I am doing small Silverlight app which will allow users to Create orders. I
I am doing a file server migration and I'm writing a small C# app
I have a small- to medium-size project that I am doing for my software
I am developing a small web app that internally authenticates users. Once the user
I've written this small app in Python that will generate paragraphs of dummy text,
I've been testing a small app I've written that basically does a http HEAD
I wanted to write a small app that would sit in my taskbar and
I'm new to node, express and jade and I'm making a small app that

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.