View Full Version : random from array
the_bob
06-05-2004, 05:39 PM
ok, i'm trying to make a deck of cards
var decksuitsk=new Array(4)
decksuits[0]="Spades"
decksuits[1]="Hearts"
decksuits[2]="Clubs"
decksuits[3]="Diamonds"
var decknumber=new Array(13)
decknumber[0]="Ace"
decknumber[1]="2"
decknumber[2]="3"
decknumber[3]="4"
decknumber[4]="5"
decknumber[5]="6"
decknumber[6]="7"
decknumber[7]="8"
decknumber[8]="9"
decknumber[9]="10"
decknumber[10]="Jack"
decknumber[11]="Queen"
decknumber[12]="King"
how would I go about getting a random one out of each of the arrays?
fredmv
06-05-2004, 06:17 PM
Array.prototype.getRandom = function()
{
return this[Math.floor(Math.random()*this.length)];
}Then simply:var foo = decksuitsk.getRandom(); // foo now contains a random element from the decksuitsk array
the_bob
06-05-2004, 07:03 PM
i must admit, i've NEVER worked with arrays before
is there a way to turn the 2 randoms into variables...?
alert("You drew the " + (randomdecknumber) + " of " + (decksuit))
or did you just tell me how to do that?
fredmv
06-06-2004, 08:37 PM
or did you just tell me how to do that?
If I understand what you're asking, yes. All I've done above is added a method onto all arrays that allows you to quickly access a random element from it. When you call this method it returns the contents of that random element. Thus, by assigning the return value to a variable you then get a random element from the array stored in the variable.
the_bob
06-06-2004, 10:52 PM
<html>
<head>
<title>Cards</title>
<script>
var decksuitsk=new Array(4)
decksuits[0]="Spades"
decksuits[1]="Hearts"
decksuits[2]="Clubs"
decksuits[3]="Diamonds"
var decknumber=new Array(13)
decknumber[0]="Ace"
decknumber[1]="2"
decknumber[2]="3"
decknumber[3]="4"
decknumber[4]="5"
decknumber[5]="6"
decknumber[6]="7"
decknumber[7]="8"
decknumber[8]="9"
decknumber[9]="10"
decknumber[10]="Jack"
decknumber[11]="Queen"
decknumber[12]="King'
Array.prototype.getRandom = function()
{
return this[Math.floor(Math.random()*this.length)];
}
var foo = decksuitsk.getRandom();
</head>
<body>
<script>
alert(foo);
</script>
</body>
</html>
would it be set up like that?
fredmv
06-06-2004, 11:20 PM
Basically, except your code has several fatal errors. I also prefer using the literal array notation. ;)Array.prototype.getRandom = function() { return this[Math.floor(Math.random()*this.length)]; }
var
deckSuits = ['Spades', 'Hearts', 'Clubs', 'Diamonds'],
deckNumber = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'],
foo = deckSuits.getRandom();
alert(foo);
the_bob
06-07-2004, 02:38 AM
<html>
<head>
<title>Cards</title>
<script>
Array.prototype.getRandom = function() { return this[Math.floor(Math.random()*this.length)]; }
var
deckSuits = ['Spades', 'Hearts', 'Clubs', 'Diamonds'],
deckNumber = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'],
foo = deckSuits.getRandom();
</head>
<body>
<script>
alert(foo);
</script>
</body>
</html>
hmm... where are these fatal errors at?
the_bob
06-07-2004, 02:44 AM
OH! I GOT IT WORKING!
thanx man, i really appreciate this
liorean
06-07-2004, 02:46 AM
Your fatal error is that you forgot the end tag for the script element in the head, for what I can see.
fredmv
06-07-2004, 03:06 AM
Your fatal error is that you forgot the end tag for the script element in the head, for what I can see.
That, and:decknumber[12]="King'Starts with a double quote yet ends with a single quote.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.