Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Return random hash table entry

    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....
    Code:
    var newOre = 0;
    
    var ore = {
    "iron" : 5,
    "silver" : 3,
    "gold" : 2,
    "platinum" : 1
    }
    
    var userOre = {
    "iron" : 0,
    "silver" : 0,
    "gold" : 0,
    "platinum" : 0
    }
    Code:
    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......?
    Last edited by nuBee; 12-16-2011 at 08:24 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Put the keynames into a separate array ... then randomize an index on this array and access the JSON object through the resulting key.

  • Users who have thanked devnull69 for this post:

    nuBee (12-16-2011)

  • #3
    New Coder
    Join Date
    Dec 2011
    Location
    Earth if you want to know
    Posts
    27
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by nuBee View Post
    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....
    Code:
    var newOre = 0;
    
    var ore = {
    "iron" : 5,
    "silver" : 3,
    "gold" : 2,
    "platinum" : 1
    }
    
    var userOre = {
    "iron" : 0,
    "silver" : 0,
    "gold" : 0,
    "platinum" : 0
    }
    Code:
    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:

    Code:
    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_


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •