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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:02:44+00:00 2026-06-08T10:02:44+00:00

I’d like something (a target) to show up five times in a random order

  • 0

I’d like something (a “target”) to show up five times in a random order (out of a total of 10 times).

The problem with the code below is I can get it to show up in a random order, but not necessarily five times:

function rand_float(min:Number,max:Number=NaN):Number {
            if (isNaN(max)) { max = min; min=0; }
            return Math.random()*(max-min)+min;
}

function rand_integer(min:Number,max:Number=NaN):int {
        if (isNaN(max)) { max = min; min=0; }
        return Math.floor(rand_float(min,max));
}
function rand_boolean(chance:Number=0.5):Boolean {
    return (Math.random() < chance);
}

var a_list:Array = ['a','b','d','e'];

var target:String = 'b';

for (var i:int = 0; i < 10; i++) {
    if (rand_boolean()) {
        trace('random other thing: ' + a_list[rand_integer(0,3)]);
    } else {
        trace('target! :' + target);
    }
}

Because the rand_boolean method is 50-50, it either “shows up” or “does not show” but there’s no guarantee it will always be 5 times out of 10.

I guess I could put in a counter and then limit it after 5 times (if it goes over) or force it to show up more (if say, after 8 times it has only shown up once) but is there perhaps a better way to go about doing this?

  • 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-08T10:02:46+00:00Added an answer on June 8, 2026 at 10:02 am

    One simple solution: create a list of possibilities with the correct amount of the desired target.

    ['a','a','b','b','b','b','b','c','c','d']
    

    Create a copy of it, remove each item as you pick them. Once you’ve pulled all 10, re-copy the list.


    Alternatively, you can create this list by seeding it with 5 ‘b’s, selecting at random 5 ‘a’,’c’, or ‘d’, and then you have your list to select from randomly, if you don’t want the non-‘b’ counts to be consistent.

    To generate the random item array:

    var targets:Array = ['b','b','b','b','b'];
    var options:Array = ['a','d','e'];
    
    var current:Array = new Array(targets);
    
    while (current.length < 10)
    {
        current.push(options[rand_integer(0,3)]);
    }
    

    And then in your random selection:

    var item:String;
    if (current.length == 0)
        //regenerate array using above code
    
    //splice(index to start at, # of items to remove) returns an array, we grab the first (only) item in it
    item = current.splice(rand_integer(0,current.length),1)[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have two tables with like below codes: Table: Accounts id | username |
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words

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.