...

View Full Version : Return random hash table entry



nuBee
12-16-2011, 09:21 PM
Having trouble figuring out how to do this...I can get the particular entry by name but I can't randomly select one from the several because it's a string and not a number....


var newOre = 0;

var ore = {
"iron" : 5,
"silver" : 3,
"gold" : 2,
"platinum" : 1
}

var userOre = {
"iron" : 0,
"silver" : 0,
"gold" : 0,
"platinum" : 0
}



function getAnotherOre () {
newOre = ore[Math.floor(Math.random() * ore.length)];
userOre += newOre;
}

The random() is for an array, tried to use it here and returns 0 every time because the key in the key:value pair is a "string", though if I'm reading it right, it is trying to get a random number based off the length of the table, so why doesn't it do that?

Ideally I want it to increment a corresponding hash table of users Ores...so if you randomly get a iron, it adds 5 to the users "iron" entry.

I had a for loop, but I only need to randomly generate 1 entry result each time called so do I need to loop through things or can I just randomly get a key:value pair back and append that to the userOre table???????

AM I APPROACHING THIS WRONG FOR WHAT I WANT TO DO?

Any ideas or link to documentation? I'm searching.....

I can get the individual value of a field manually: console.log("keyName"); which will output the right number, but how do I get a random("keyName") and once I figure that out I probably would append("correspondingName", value) to the other table...

Basically, functionally I just want to randomly get one of several values when the function is called. I guess I could make global variables for each type of ore, then just manually increment a random one some other way...

This is for a html5 game where when you collide with a "mine" it triggers the function but I want it to randomly give various types of ore instead...Maybe I'm going about it wrong...

Maybe it's easier just to declare 10 different global variables (assuming 10 ore types) and randomly give a number 0-10 of each every time the function is called......?

devnull69
12-16-2011, 10:06 PM
Put the keynames into a separate array ... then randomize an index on this array and access the JSON object through the resulting key.

cuzMazn
12-16-2011, 10:12 PM
Having trouble figuring out how to do this...I can get the particular entry by name but I can't randomly select one from the several because it's a string and not a number....


var newOre = 0;

var ore = {
"iron" : 5,
"silver" : 3,
"gold" : 2,
"platinum" : 1
}

var userOre = {
"iron" : 0,
"silver" : 0,
"gold" : 0,
"platinum" : 0
}



function getAnotherOre () {
newOre = ore[Math.floor(Math.random() * ore.length)];
userOre += newOre;
}
...

Hi nuBee,

How about assigning a new Array which contains key value for the Object, then use random index to access random key name, and use them in the object?

Here is the simple form:



var $ore = {
iron: 0,
silver: 0,
gold: 0,
platinum: 0
},

$keyArray = ["iron", "silver", "gold", "platinum"],

//Use Math.round(Math.Random() * $keyArray.length) to receive a random index between 0 and length of the Array
//In this case, random from 0 to 4
$oreKey = $keyArray[Math.round(Math.Random() * $keyArray.length)];

alert("Random $ore is " + $ore[$oreKey]);


Tim_



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum