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

  • Home
  • SEARCH
  • 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 8439755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:05:20+00:00 2026-06-10T08:05:20+00:00

For example I have 3 elements in an array: public function randomTile():Number { var

  • 0

For example I have 3 elements in an array:

public function randomTile():Number

{

var tiles:Array = new Array(fire,ice,water);

var index:Number=Math.floor(Math.random()*tiles.length);

return tiles[index];

}

How to set the chances of occurrence of fire(70%), ice(10%), and water(20%)?

  • 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-10T08:05:22+00:00Added an answer on June 10, 2026 at 8:05 am

    This should work for any number of elements and you can specify any chance value.

    var tiles:Array = [
      {"item":"fire", "chance":70 },
      {"item":"ice",  "chance":10 },
      {"item":"water","chance":20}
    ];
    
    var picked:Object = pickRandomByChance(tiles);
    trace(picked.item);
    
    public function pickRandomByChance(options:Array):Object
    {
      var copy:Array = [];
      var range:Number = 0;
      for (var i:int = 0; i < options.length; i++) 
      {
        copy.push( { "item":options[i].item, "chance":options[i].chance } );
        range += copy[i].chance;
        if (i > 0)
          copy[i].chance += copy[i - 1].chance;
      }
    
      var pick:Number = Math.floor(Math.random() * range);
    
      for (i = 0; i < copy.length; i++)
      {
        if (pick <= copy[i].chance)
          return copy[i];
      }
      return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how i can duplicate one element from array: for example, i have this array:
For example, I have two elements in an enum. I would like the first
I have a site with absolute positioned elements on it, for example the top
For example, I have a bunch of elements inside a #dialogbox div, can I
Does Boost Ranges have a built-in way to cout the elements easily, for example
Does clojure have a powerful 'loop' like common lisp. for example: get two elements
I have a 1-dimensional array that fills up a table of 40 random elements
I have a class that holds an array of elements, and I want to
When I am writing for example Spring Bean I often have empty element without
How would I split a node/element at a position (selection). Example I have this

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.