Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

Random Numbers

Locked
ruiascensao
Posts: 177
Joined: Tue Nov 15, 2011 2:24 pm

Random Numbers

Unread post by ruiascensao »

Hello,

I'm triying to generate random numbers from a field. The user enters a number in the field sample_size in the table sample (example: 90) and when saving for the first time (and just for the first time) nuBuilder will fill the sample_num with 40 not repeated random numbers (Example: 5,67,32,78,90, ...).
This depends on the number entered from the user:

From 1 to 8: 8 numbers
From 8 to 15: 15 numbers
From 16 to 25: 25 numbers
From 26 to 50: 25 numbers
From 51 to 90: 40 numbers
From 91 to 150: 60 numbers
From 151 to 280: 105 numbers
From 281 to 500: 170 numbers
From 501 to 1200: 300 numbers
From 1201 to 3200: 200 numbers

Can I do this using SQL ou javascript?

Best regards,
Rui
BR
Rui
ruiascensao
Posts: 177
Joined: Tue Nov 15, 2011 2:24 pm

Re: Random Numbers

Unread post by ruiascensao »

Hello,

I'm using the following code (not completed for my porpose):

Code: Select all

function randon_sample() {
	units = document.getElementById('batch_size').value);
	sample = document.getElementById('batch_smp').value);
	var smp = [];
	if ( sample =='0'&& units<=8 {
		for (var i=0; i<8; ++i) {
		if ((i % 10) == 0) { tarr.push('\n'); }
		smp.push(Math.floor(Math.random()*units)+1);
 	}
  }
  if ( sample =='0'&& (units>8 && units<=15) {
		for (var i=0; i<8; ++i) {
		if ((i % 10) == 0) { tarr.push('\n'); }
		smp.push(Math.floor(Math.random()*units)+1);
	}
  }
  if ( sample =='0'&& (units>15 && units<=25) {
		for (var i=0; i<8; ++i) {
		if ((i % 10) == 0) { tarr.push('\n'); }
		smp.push(Math.floor(Math.random()*units)+1);
	}
  }
  document.getElementById('batch_smp').value = smp.join(',');

}
I have the code on the form batch and call it from the batch_smp field.
But the form went blank!
What am I doing wrong?

Regards,
Rui
BR
Rui
ruiascensao
Posts: 177
Joined: Tue Nov 15, 2011 2:24 pm

Re: Random Numbers

Unread post by ruiascensao »

Hi,

I have change the code and in a normal html page would be like this:

Code: Select all

<!DOC HTML>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
//<![CDATA[
var smp = [];

function fill_smp(units) {
  smp = [];
  for (var i=0; i<units; ++i) { smp.push(i+1); }
  smp = smp.sort(randOrd);
}

function randOrd() {
  return (Math.round(Math.random())-0.5);
}

function padLeft(value,size) {  // to make pretty
  value = value.toString();
  while (value.length < size) { value = ' '+value; }
  return value;
}

function randon_sample() {
  units = document.getElementById('batch_size').value;

  if ( units>1200 && units<=3200 ) { fill_smp(200); }
  if ( units>500 && units<=1200 ) { fill_smp(300); }
  if ( units>280 && units<=500 ) { fill_smp(170); }
  if ( units>150 && units<=280 ) { fill_smp(105); }
  if ( units>90 && units<=150 ) { fill_smp(60); }
  if ( units>50 && units<=90 ) { fill_smp(40); }
  if ( units>25 && units<=50 ) { fill_smp(25); }
  if ( units>15 && units<=25 ) { fill_smp(25); }
  if ( units>8 && units<=15 ) { fill_smp(15); }
  if ( units>0 && units<=8 ) { fill_smp(8); }

  var str = '';
  if ( (units<=0) || (units >3200) ) {
    alert('Sample size out of range'); 
  } else {
// alternate display
//    document.getElementById('batch_smp').value = smp.join(',');
    for (var i=0; i<smp.length; i++) {
      str += padLeft(smp[i],3)+',';
      if ((i%10) == 9) { str += '\n'; }
    }
  }
  document.getElementById('batch_smp').value = str;
}
//]]>
</script>
</head>
<body>
<input type="text" id="batch_size" value='10'>Size<br>
<button onclick="randon_sample()">Recalc</button><br>
<textarea id="batch_smp" rows="20" cols="50"></textarea>
</body>
</html>
I need your help to use it in nubuilder.
Best Regards,
Rui
BR
Rui
admin
Site Admin
Posts: 2781
Joined: Mon Jun 15, 2009 2:23 am
nuBuilder Version: 4.5
Been thanked: 1 time

Re: Random Numbers

Unread post by admin »

Rui,

Try something like this in the JavaScript part of the form..

Code: Select all


function nuLoadThis(){

    if(document.getElementById('recordID').value == '-1'){

         randon_sample();

    }

}

Steven
ruiascensao
Posts: 177
Joined: Tue Nov 15, 2011 2:24 pm

Re: Random Numbers

Unread post by ruiascensao »

Thank to all !!
It's working now.

Best Regards,
Rui
BR
Rui
admin
Site Admin
Posts: 2781
Joined: Mon Jun 15, 2009 2:23 am
nuBuilder Version: 4.5
Been thanked: 1 time

Re: Random Numbers

Unread post by admin »

.
Locked