Skip navigation
Currently Being Moderated

Random number generation based on different random number.

Apr 30, 2012 10:22 AM

Im just looking for a little bit of input or possibly another approach to what I'm trying to accomplish.

 

I am in the early stages of creating a small application/activity to simulate taking someone's blood pressure. My base logic is sound, and the seems to be functional, however, it feels like ive pretty much "hacked" it to get it to work.

 

 

So here is the code:


// 90 - 220  
 
var systolic:uint = (Math.floor((Math.random() *130) + 90)); 
// 50 - 110 and ~diference of at least 50 //= (Math.floor((Math.random() * 60 )+ 50));

 
var diastolic:uint 
 
 
 // sets lower number
function setDiastolic(){
 
      var dias:uint = (Math.floor((Math.random() * 60 )+ 50));
 
      if (systolic - dias < 50){
  
            while (systolic - dias < 50){
                 
                dias = (Math.floor((Math.random() * 60 )+ 50));
  
            }
      }
 
      diastolic = dias;
 };

 

The issue I am having is with the setDiastolic function and the "if" and "while": 

 

With both the "if" and "while" the code works 100% of the time, It just feels redundant and awkward.

 

When I remove the "if" statement the code works about 85% of the time. However, sometimes the code times out on me.

 

Like I said, Using both the "if" and "While" works. It just seems like a "patchwork" solution as well as a good learning opportunity for me, so I decided to throw the question out there. Is there a better way to resolve this? Possibly a different approach I could take to accomplish the same thing?

 

 

Thanks in advance.

 

m.

 
Replies
  • Currently Being Moderated
    Apr 30, 2012 10:39 AM   in reply to MATTANDIE

    Maybe if you can describe your base logic and what you are trying to accomplish it will be easier to see if there's a better way.  As is, I don't see realistic reasoning being applied to relating systolic to diastolic, just tossing out random values and waiting for them to survive the relationship of having a difference >= 50.  While your design be considered valid if s=220 and d= 50,  I'd be hoping for an ambulance to arrive very soon or BP meter repairman.

     

    Too often when one uses a while loop they take the chance of running a seemingly infinite loop.  In your case where there are random values being relied on, there is every possibility of running into a scenario where it is more difficult to find a solution due to the randomness.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 30, 2012 2:10 PM   in reply to MATTANDIE

    You can probably avoid the while loop if you use the systolic value as part of your random calculation though you probably need to set up conditions based on the systolic value... one of which would be a case where the systolic cannot be less than 100 regardless of the 90-220 range you say it can have...  If it is between 90 and 100 chances are that is when your while loop is infinitely looping... failing to find a diastolic value that is both > 50 (the minimum diastolic value allowed) and <50 (the need to be 50 below systolic) at the same time.

     

    Since you get the systolic value first, you should be able to use it to set the allowed range of your diastolic. 

     

    var systolic:uint = (Math.floor((Math.random() *130) + 90));

     

    var diaMax = systolic -50; // but limited to 110

     

    That last line is just food for thought.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points