0

How tocreate a random number between 0.3 and 1.

I know that Math.random() creates a number between 0 and 1.

However im using css scale and below 0.3 is to small.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Matt Hammond
  • 765
  • 1
  • 7
  • 25
  • JQuery does not have any native support for random... So you really can't. – Alexei Levenkov Jul 11 '15 at 01:33
  • 1
    @AlexeiLevenkov jQuery does not *need* "native support for random", because `Math.random` works just fine. ;-) – zwol Jul 11 '15 at 01:35
  • 1
    Yeah dont mark him down for asking an honest question. Probability new jquery. – Andrew Kralovec Jul 11 '15 at 01:43
  • See http://stackoverflow.com/questions/4959975/generate-random-value-between-two-numbers-in-javascript – guest271314 Jul 11 '15 at 01:49
  • @zwol OP clearly knows how to get random number with JavaScript. There is possiblye something special about need to use JQuery, otherwise it is duplicate of http://stackoverflow.com/questions/4959975/generate-random-value-between-two-numbers-in-javascript (which it apparently is based on OP's comment to strict JavaScript answer). – Alexei Levenkov Jul 11 '15 at 01:51

2 Answers2

4

If you want an equal chance of any number over the range 0.3 to 1, then

Math.random() * 0.7 + 0.3

Multiplying a number from 0 to 1 by 0.7 will give you a number from 0 to 0.7, and the + 0.3 will then get you to the range 0.3 to 1.0.

DLH
  • 557
  • 5
  • 13
  • 1
    @MattHammond if this is the correct answer, then you should modify your question and remove the jQuery part. – DA. Jul 11 '15 at 02:04
0

If you wanted to get between 0.3 and 1, you would put:

var min = 0.3,
    max = 1,
    var answer = (Math.random() * (max - min) + min).toFixed(4);
    alert(answer); 

0.3 is an our start number1 is a number of possible results Or

(Math.random() * (1 - 0.3) + 0.3).toFixed(4)

toFixed is used to convert a number into a string,

Andrew Kralovec
  • 491
  • 4
  • 13