Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by chelnok · Feb 12, 2013 at 07:31 PM · javascriptrandomdictionaryelement

dictionary, random element (js)

Cant figure out how to get random element from dictionary. Coding with javascript ..unityscript ..or whateverScript that is at the moment. PleaseHelpMe.

edit: what is easiest way to get random element from dictionary.

Comment
Add comment · Show 3
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 chelnok · Feb 12, 2013 at 10:40 PM 0
Share

Why downvote?

avatar image flaviusxvii · Feb 12, 2013 at 10:47 PM 1
Share

Probably because it's trivially easy..

dictionary[dictionary.keys[Random.Range(0, dictionary.Count)]]

Something like that.

avatar image chelnok · Feb 13, 2013 at 02:43 AM 0
Share

Probably :/

Anyway, if you know how its done, please tell me. At the moment i'm using for loop to create new array from keys :)

1 Reply

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

Answer by dubbreak · Feb 13, 2013 at 03:46 AM

Ok, so problem is you can't do exactly what flaviusxvii suggested because you can't index into a keycollection. Of course with linq extension methods you can.

Here are two ways:

 using System.Linq;
 using System.Collections.Generic;
 
 //create dictionary and add stuff to it
         Dictionary<string,bar> foobarDict = new Dictionary<string, bar>()
         {
             {"keyone", new bar()},
             {"keytwo", new bar()}
         };

//solution 1:

         var randomKey = foobarDict.Keys.ElementAt((int)Random.Range(0,foobarDict.Keys.Count -1));
         var randomValueFromDictionary = foobarDict[randomKey];
 
 

//solution 2:

         var randomKey = foobarDict.Keys.ToArray()[(int)Random.Range(0,foobarDict.Keys.Count -1)];
         var randomValueFromDictionary = foobarDict[randomKey];

If you use the ToArray it would probably be best to cache the array rather than calling that repeatedly (especially if your dictionary stays static). I'm not sure of the overhead, but it really couldn't hurt. For all I know ElementAt is converting the collection datatype to an array as well before indexing into it. I can't be bothered to look that up. If performance is a concern look it up.

Of course it'd be best to abstract this into a method as well.

Comment
Add comment · Show 8 · 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 flaviusxvii · Feb 13, 2013 at 05:11 AM 0
Share

Yeah, that's the idea I was getting at. I question the sanity of wanting to do this in the first place though. I've never run across a need to randomly choose an element out of an associative data structure.

avatar image dubbreak · Feb 13, 2013 at 05:17 AM 0
Share

Totally. You had it pretty much on the head (should have gotten most people there). It's basically the same as selecting a random item out of an array. To make it look nicer a extension method could be used. So in effect you have:

 var randomValue = roobarDict[foobarDict.$$anonymous$$eys.Random()];

That'd look nicer.

Anyhow I've never run into the need either. $$anonymous$$akes me wonder if it's the right data type. If the key is never directly accessed then an array of the values would be better. Of course since we don't really care about the key could just do:

 var randomValueFromDictionary = foobarDict.Values.ToArray()[(int)Random.Range(0,foobarDict.Values.Count -1)];

Or something along those lines (convert the values to an array or just index into the values). You no longer need the association when picking something random.

avatar image flaviusxvii · Feb 13, 2013 at 05:26 AM 1
Share

Yeah, just the nature of the collection. I don't want random stuff out of a dictionary. I want to use a key to get exactly what I put in the thing.

avatar image chelnok · Feb 13, 2013 at 09:13 PM 0
Share

Thank you very much. Your answer is good ..almost. Your examples does not actually work. So far I've only been able to get ToArray work properly. If you can make those work with javascript, your answer will be accepted. I believe you have some mixed syntax there? Few obvious #c syntax like using (in js i know it is import).

avatar image chelnok · Feb 13, 2013 at 09:23 PM 0
Share

@flaviusxvii You are right about nature of the collection. Sure i could explain my messy structure of few thousand lines of code and why this is how it is, but then again probably you dont want to know. This is my first game done with unity. This is for learning you know :) And for now i just need to know correct syntax.

Show more comments

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create random movement in 2D 2 Answers

Display random element from dictionary 1 Answer

#define directive in UnityScript 1 Answer

Sound playing at random. (JS) 2 Answers

Health script and damage script dont work. 2 Answers


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