I’ve created a class in coffeescript with a randomInt method that generates x and y instance variables. However when I create objects from this class the x and y values are the different but consistent for both.
Here is the code to demonstrate: http://jsfiddle.net/paulmason411/BvPBG/
class Shape
getRandomInt = (min, max) ->
Math.floor(Math.random() * (max - min + 1)) + min
y: getRandomInt(1,100)
x: getRandomInt(1,100)
shape1 = new Shape
shape2 = new Shape
alert(shape1.x)
alert(shape2.x)
alert(shape1.y)
alert(shape2.y)
I need each alerted value to be different.
I searched for a solution and in other programming languages they use srand() however js doesn’t have this native function.
Create “instance variables” of
xandy(the@makes them such vars):which printed:
Note that the
getRandomIntfunction is added toShape.prototype, andShape::getRandomInt(1,100)is identical toShape.prototype.getRandomInt(1,100).