nix
nix

Reputation: 2285

Generating random number from 0 to 10 including both

I heard of some Math.random() but I'm not sure about using it correctly. Number must be whole, so I suppose I'll need some rounding function.

Upvotes: 0

Views: 2554

Answers (3)

Mattias
Mattias

Reputation: 3907

Math.random() returns a number between 0 - 1. Multiply it with 10 an round it using an int.

// random number between 1- 10
var randomRounded : int = Math.round(Math.random() * 10);

Edit: more accurate version

// more accurate
var randomRounded : int = Math.floor(Math.random() * 11);

Upvotes: 0

Creynders
Creynders

Reputation: 4583

Math.random produces a pseudo random number between [0;1[ (0 included, but 1 excluded) To have an evenly distributed probability for all numbers between 0 and 10, you need to:

var a : int = Math.floor( Math.random() * 11 )

Upvotes: 3

Gianpaolo Di Nino
Gianpaolo Di Nino

Reputation: 1147

Math.round(Math.random()*10); maybe?

Upvotes: 0

Related Questions