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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:48:24+00:00 2026-06-13T01:48:24+00:00

Assuming that Math.random() produces evenly distributed random numbers between 0 and 1, is this

  • 0

Assuming that Math.random() produces evenly distributed random numbers between 0 and 1, is this a correct implementation of the Fischer Yates shuffle? I am looking for a very random, even distribution, where the number of shuffled elements in an input array (arr) can be specified (as required).

shuffle = (arr, required)->
  rnd = (int) ->
    r = Math.random() * int
    Math.round r

  len = arr.length-1

  for i in [len..1]
    random = rnd(i)
    temp = arr[random]
    arr[random] = arr[i]
    arr[i] = temp
    break if i < len - (required - 2)

  return arr
  • 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-13T01:48:25+00:00Added an answer on June 13, 2026 at 1:48 am

    A couple things:

    • Rather than Math.round(), try Math.floor(); in your
      implementation Math.round() gives the first element (at index 0)
      and the last element less of a chance than all the other elements
      (.5/len vs. 1/len). Note that on the first iteration, you input arr.length - 1 for arr.length elements.
    • If you’re going to have a required
      variable, you might as well make it optional, in that it defaults to the length of the array: shuffle = (arr,
      required=arr.length)
    • You return the entire array even though you only shuffled the last elements. Consider instead returning arr[arr.length - required ..]
    • What if required isn’t in the range [0,arr.length]?

    Putting it all together (and adding some flair):

        shuffle = (arr, required=arr.length) ->
          randInt = (n) -> Math.floor n * Math.random()
          required = arr.length if required > arr.length
          return arr[randInt(arr.length)] if required <= 1
    
          for i in [arr.length - 1 .. arr.length - required]
            index = randInt(i+1)
            # Exchange the last unshuffled element with the 
            # selected element; reduces algorithm to O(n) time
            [arr[index], arr[i]] = [arr[i], arr[index]]
    
          # returns only the slice that we shuffled
          arr[arr.length - required ..]
    
        # Let's test how evenly distributed it really is
        counter = [0,0,0,0,0,0]
        permutations = ["1,2,3","1,3,2","2,1,3","2,3,1","3,2,1","3,1,2"]
        for i in [1..12000]
          x = shuffle([1,2,3])
          counter[permutations.indexOf("#{x}")] += 1
    
        alert counter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that I have this: enum { A = 0x2E, B = 0x23, C
Assuming that I have this navigation bar <div id = nav> <a class =
Assuming that I have objects similar to this: public class User { public int
Assuming that parsing the equation would not be a problem, how can I make
Assuming that the larger a database gets, the longer it will take to SELECT
assuming that I know the PID of a process and want to do a
Assuming that I have a CronTriggerBean similar to <bean id=midMonthCronTrigger class=org.springframework.scheduling.quartz.CronTriggerBean> <property name=jobDetail ref=reminderJobDetail
Assuming that best practices have been followed when designing a new database, how does
Assuming that I have a typedef declared in my .h file as such: typedef
Assuming that I know there is a git-daemon running at git://git.mycompany.com , how can

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.