- Home /
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.
Probably because it's trivially easy..
dictionary[dictionary.keys[Random.Range(0, dictionary.Count)]]
Something like that.
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 :)
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.
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.
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.
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.
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).
@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.
Your answer
Follow this Question
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