Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by headkit · May 03, 2010 at 10:01 AM · arrayrandom

randomizing arrays

hi folks!

is there a better way to randomize an array like the following one?

theArray = randomizeArray(theArray);

function randomizeArray(arr : Array) : Array { var counter : int = arr.length; var reArr = new Array();

 while (counter-- >= 1)
 {
     var rndM : int = Random.Range(0, arr.length-1);
     reArr.push(arr[rndM]);
     arr.RemoveAt(rndM);
 }

 return reArr;

}

thnx!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spinaljack · May 03, 2010 at 11:37 AM 0
Share

Why do you need to make a new array? Can't you just read the elements in a random order?

avatar image Molix · May 03, 2010 at 11:43 AM 0
Share

$$anonymous$$y JS is a bit hazy, but it looks like it empties the original array, which is fine if you are setting the return value to the passed array, but if you did array2=randomizeArray(array1), then array1 would be empty, which is a bit of a harsh side-effect.

2 Replies

· Add your reply
  • Sort: 
avatar image
25
Best Answer

Answer by duck · May 03, 2010 at 01:23 PM

Here's a slightly simpler version which doesn't create a new array, or add/remove elements to the array. It simply swaps elements around in the original array, giving a very fast algorithm which is useful if this happens to be part of some speed-critical code. (this is called the Fisher Yates shuffle).

static function RandomizeArray(arr : Array)
{
    for (var i = arr.length - 1; i > 0; i--) {
        var r = Random.Range(0,i);
        var tmp = arr[i];
        arr[i] = arr[r];
        arr[r] = tmp;
    }
}

Because this works on the original array, it does not return a result. To use, you would simply call:

RandomizeArray(myArray);

In addition, because this function doesn't rely on adding or removing items, it can also be adapted to work with built-in arrays rather than Unity's Javascript Arrays by simply changing the input parameter type to Object[] rather than Array, and by writing ".length" as ".Length" (change the lower case l to a capital L). Eg:

static function RandomizeBuiltinArray(arr : Object[])
{
    for (var i = arr.Length - 1; i > 0; i--) {
        var r = Random.Range(0,i);
        var tmp = arr[i];
        arr[i] = arr[r];
        arr[r] = tmp;
    }
}
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ai_vierus · Apr 23, 2014 at 03:59 AM 0
Share

have u script in c#?

avatar image ai_vierus · Apr 23, 2014 at 04:00 AM 0
Share

Can someone please convert it in c sharp. i am new to unity.

avatar image vipaware · Aug 10, 2014 at 06:31 PM 0
Share

Sorry for late answer. Here is a C# sample of the code above, as ai_vierus asked. It's a generic method that will shuffle passed built-in array of any type in a very efficient way (Fisher-Yates shuffle).

 public static void ShuffleArray<T>(T[] arr) {
   for (int i = arr.Length - 1; i > 0; i--) {
     int r = Random.Range(0, i);
     T tmp = arr[i];
     arr[i] = arr[r];
     arr[r] = tmp;
   }
 }

It doesn't have return value, ins$$anonymous$$d it changes the parameter passed. Use this method as below:

 ShuffleArray<int>(arr); // in case built-in array is of type int


or via a simpler call:

 ShuffleArray(arr);
avatar image -JoSeM- · Aug 14, 2014 at 09:17 AM 1
Share

The function you wrote doesn't randomize, so its not a good function.

Think a bit. Your function just moves the elements and fixes in a position. $$anonymous$$y english is not so good, so I'll expose the problems I've seen with an example. Example: Array("A","B","C");

If you use your function, it'll the the next: 1st step: Grab C (position 3) and save it on tmp 2nd step: Grab A or B and move it to position 3 3rd step: Save C on the empty position Actual array: ("C","B","A") or ("A","C","B") 4th step: Grab position 2 and move it to 1 or viceversa (it's the same)

Final Array: ("B","C","A") or ("C","A","B")

Your randomize function just give 2 solutions!!!!!

There are 6 posible solutions randomizing that array. ("A","B","C") ("A","C","B") ("B","A","C") ("B","C","A") ("C","A","B") ("C","B","A")

So I would say, this randomize function doesnt work as espected, never! If you call the function twice, it get back to the original array. ("B","C","A") -> ("","C","B")->("A","C","B")->("C","A","B") (it's one of your array solutions) ("B","C","A") -> ("B","","C")->("B","A","C")->("A","B","C") -> original And the same for("C","A","B") array. You need to create a new array, like this:

 static function RandomizeArray(arr : Array){
 var thisarray = new Array(arr);
 var temp = Array();
 for (var i = arr.Count - 1; i >= 0; i--) {
 var r = Random.Range(0,i+1);
 temp.Add(thisarray[r]);
 thisarray.RemoveAt(r);
 }
  
 for (i = 0; i < arr.Count; i++) {
 arr[i] = temp[i];
 }
 }
avatar image Oliphant · Sep 20, 2014 at 03:10 PM 6
Share

-JoSe$$anonymous$$- is correct that the provided code is not producing all possible permutations, but it's not because the algorithm is fundamentally flawed. Rather, there's a bug in the code. Random.Range(int $$anonymous$$, int max) returns "a random integer number between $$anonymous$$ [inclusive] and max [exclusive]." - source The exclusion of the max number has bitten me a number of times (the float version of the Random.Range function is inclusive for both $$anonymous$$ and max, so it's easy to forget about this subtlety when switching to the integer version).

So the code for the in-place shuffle should look like this in C#:

 public static void ShuffleArray<T>(T[] arr) {
   for (int i = arr.Length - 1; i > 0; i--) {
     int r = Random.Range(0, i + 1);
     T tmp = arr[i];
     arr[i] = arr[r];
     arr[r] = tmp;
   }
 }

Likewise, the JavaScript version should include the same change:

 static function RandomizeBuiltinArray(arr : Object[])
 {
     for (var i = arr.Length - 1; i > 0; i--) {
         var r = Random.Range(0, i + 1);
         var tmp = arr[i];
         arr[i] = arr[r];
         arr[r] = tmp;
     }
 }

avatar image sunderplugs11 Oliphant · Aug 21, 2016 at 01:12 PM 0
Share

ty man! amazing

Show more comments
avatar image
1

Answer by -JoSeM- · Aug 14, 2014 at 09:34 AM

I hope this will do the job you need

 static function RandomizeArray(arr : Array){
     var thisarray = new Array(arr);
     var temp = Array();
     for (var i = arr.Count - 1; i >= 0; i--) {
         var r = Random.Range(0,i+1);
         temp.Add(thisarray[r]);
         thisarray.RemoveAt(r);
     }
     
     for (i = 0; i < arr.Count; i++) {
         arr[i] = temp[i];
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Aug 14, 2014 at 09:22 AM 0
Share

code tags would be even more helpful... ;)

avatar image -JoSeM- · Aug 14, 2014 at 04:39 PM 0
Share

do you mean, explaining how does the function work?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making an Array of Animations Play at Random With No Repeats 3 Answers

Simple array an spawning question 6 Answers

Puzzle + Grid Instantiate - Random 2 Answers

Random Children Array that changes Parent, not working right 0 Answers

Randoim values in a dynamic array C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges