-2

I want to generate random numbers between 0.0251 to 0.4070.

From below code:

<circle> r value has to be dynamically set as 0.0841, 0.2541, 0.3257, 0.0998 for each attribute..

SVG Code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
 <?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="660" height="320" viewBox="0 0 201 97">
  <g stroke="#000" stroke-width=".144">
    <circle class="st3" cx="191" cy="90" r=".3"/>
    <circle class="st3" cx="190" cy="90" r=".3"/>
    <circle class="st3" cx="189" cy="90" r=".3"/>
    <circle class="st3" cx="192" cy="89" r=".3"/>
    <circle class="st3" cx="191" cy="89" r=".3"/>
    <circle class="st3" cx="190" cy="89" r=".3"/>
    <circle class="st3" cx="193" cy="88" r=".3"/>
    <circle class="st3" cx="192" cy="88" r=".3"/>
    <circle class="st3" cx="178" cy="88" r=".3"/>
    <circle class="st3" cx="177" cy="88" r=".3"/>
    <circle class="st3" cx="194" cy="87" r=".3"/>
    <circle class="st3" cx="179" cy="87" r=".3"/>
    <circle class="st3" cx="178" cy="87" r=".3"/>
  </g>
</svg>

jQuery

$("circle.st3").attr("r", (Math.random() * (0.0251 - 0.4070) + 0.4070).toFixed(4));

Online Demo

Output from Above Example:

<g stroke="#000" stroke-width=".144">
    <circle class="st3" cx="191" cy="90" r="0.0973"></circle>
    <circle class="st3" cx="190" cy="90" r="0.0973"></circle>
    <circle class="st3" cx="189" cy="90" r="0.0973"></circle>
    <circle class="st3" cx="192" cy="89" r="0.0973"></circle>
    <circle class="st3" cx="191" cy="89" r="0.0973"></circle>
    <circle class="st3" cx="190" cy="89" r="0.0973"></circle>
    <circle class="st3" cx="193" cy="88" r="0.0973"></circle>
    <circle class="st3" cx="192" cy="88" r="0.0973"></circle>
    <circle class="st3" cx="178" cy="88" r="0.0973"></circle>
    <circle class="st3" cx="177" cy="88" r="0.0973"></circle>
    <circle class="st3" cx="194" cy="87" r="0.0973"></circle>
    <circle class="st3" cx="179" cy="87" r="0.0973"></circle>
    <circle class="st3" cx="178" cy="87" r="0.0973"></circle>
  </g>

But my requiremnt is to display different number for each "r" attribute..

Can somebody please help me out?

Reddy
  • 1,477
  • 29
  • 79
  • 2
    Please do a search before asking on SO. Generating random numbers within a specific range is a very commonly solved problem. – Cerbrus Jun 17 '15 at 14:35
  • @Cerbrus, the page you linked to is for finding random _integers_ within a specific range. – roscioli Jun 17 '15 at 14:43
  • @roscioli: No it's not. Did you even look at the `getRandomArbitrary` function in the accepted answer there? – Cerbrus Jun 17 '15 at 14:44
  • @Cerbrus Yes, you are correct, my bad. But wouldn't this be a more appropriate duplicate link: http://stackoverflow.com/questions/17726753/get-a-random-number-between-0-0200-and-0-120-float-numbers?lq=1? – roscioli Jun 17 '15 at 14:47
  • Nope, but that one is a duplicate of http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range – Cerbrus Jun 17 '15 at 14:48
  • http://stackoverflow.com/questions/1969240/mapping-a-range-of-values-to-another might help with your math problem if there is one – LionC Jun 17 '15 at 14:52
  • **@Cerbrus**, my question doesn't seems to be duplicate... It is showing same number for all the circle's **r** value... Eg: 0.02145 for all the elements :( I want to display different numbers for each circle not the same number to all... Hi All, Above referred links are not working... **Am I missing something?** Please check the code... `$("circle.st3").attr("r", (Math.random() * (0.120 - 0.0200) + 0.0200).toFixed(4));` http://codepen.io/anon/pen/waqOLO – Reddy Jun 18 '15 at 04:07
  • Your question is 2 questions in 1: First, generating random numbers. Second, how to set them in your HTML. – Cerbrus Jun 18 '15 at 06:23

1 Answers1

1

Simple:

var upperLimit = 0.4070;
var lowerLimit = 0.0251;
var yourResult = lowerLimit + Math.random() * (upperLimit - lowerLimit);
roscioli
  • 1,200
  • 3
  • 15
  • 33
  • somehow, this seems to be not working.. can you please give me fiddle or Snippet please? – Reddy Jun 17 '15 at 14:36