# Javascript random number

Dear Sir,
I have a table with many coloumns with one coloumn as vacant corresponding to every row.All coloumns are retreived from database.I have to generate random numbers corresponding to every row.But problem is one constraint is there while generating random numbers.If there is 60 rows,i have to do 60/10=6 groups of those rows.group-1,2,3,4,5,6.Under one group there will be 10 rows and their random numbers will be same.Then next 10 rows' random numbers will be same.coloumn name is LOT NO.After displaying that table with that vacant coloumn,then there will be one button generate lot no.After clickin on that button numbers will be generated along with updation in the database for the LOT_NO coloumn in the database.

e.g:-I have a table for students' details in a school.unique is their roll number in rows.coloumns are age,sex,grade etc.There is one coloumn vacant for lot no.I want to place equal number of students under one lot number.Then same numbers under another.If there is 60 stuednts.Group them as a,b,c,d,e,f.six group.60/10=6.For the first 10 studets lot no should be generated randomly and should be same.Next 10 students should have same lot no with random generation.I want that random function with its implementation.After clicking the button javascript should create random numbers as i have mentioned above.
Age Sex Location Lot No
Roll No
452345 4 M ty Empty
545454 6 M 5y Empty
767568 7 M hy Empty
203392 9 M ty Empty

After clickig on generate numbers random numbers should be generated.But First two students should have same random numbers and second two should have another same numbers.Likewise we can create for 20,30,40 students.

Regards
Debasis Mohapatra

July 13, 2010 at 5:58 PM

Hi Friend,

Try the following code:

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%!
public int nullIntconvert(String str){
int num=0;
if(str==null) {
str="0";
}
else if((str.trim()).equals("null")) {
str="0";
}
else if(str.equals("")) {
str="0";
}
try{
num=Integer.parseInt(str);
}
catch(Exception e) { }
return num;
}
%>
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
ResultSet rs1 = null;
ResultSet rs2 = null;
PreparedStatement ps1=null;
PreparedStatement ps2=null;

int showRows=10;
int totalRecords=10;
int totalRows=nullIntconvert(request.getParameter("totalRows"));
int totalPages=nullIntconvert(request.getParameter("totalPages"));
int iPageNo=nullIntconvert(request.getParameter("iPageNo"));
int cPageNo=nullIntconvert(request.getParameter("cPageNo"));

int startResult=0;
int endResult=0;
if(iPageNo==0){
iPageNo=0;
}
else{
iPageNo=Math.abs((iPageNo-1)*showRows);
}
String query1="SELECT SQL_CALC_FOUND_ROWS * FROM student limit "+iPageNo+","+showRows+"";
ps1=conn.prepareStatement(query1);
rs1=ps1.executeQuery();
String query2="SELECT FOUND_ROWS() as cnt";
ps2=conn.prepareStatement(query2);
rs2=ps2.executeQuery();
if(rs2.next()) {
totalRows=rs2.getInt("cnt");
System.out.println(totalRows);
}
%>
<html>
<h3>Pagination of JSP page</h3>
<body>
<form method="post" action="update.jsp">
<input type="hidden" name="iPageNo" value="<%=iPageNo%>">
<input type="hidden" name="cPageNo" value="<%=cPageNo%>">
<input type="hidden" name="showRows" value="<%=showRows%>">
<table width="100%" cellpadding="0" cellspacing="0" border="1" >
<tr>
<td>Roll No</td>
<td>Name</td>
<td>Marks</td>
<td>Lot No</td>
</tr>
<%
Random r=new Random();
int ran=r.nextInt(10);
while(rs1.next()){
%>
<tr>
<td><input type="text" name="roll" value="<%=rs1.getInt("rollNo")%>"></td>
<td><input type="text" name="name" value="<%=rs1.getString("name")%>"></td>
<td><input type="text" name="marks" value="<%=rs1.getInt("marks")%>"></td>
<td><input type="text" name="lotNo" value="<%=ran%>"></td>
</tr>

July 13, 2010 at 5:59 PM

continue..

<%
}
%>
<%
try{
if(totalRows<(iPageNo+showRows)) {
endResult=totalRows;
}
else{
endResult=(iPageNo+showRows);
}
startResult=(iPageNo+1);
totalPages=((int)(Math.ceil((double)totalRows/showRows)));
}
catch(Exception e){
e.printStackTrace();
}
%>
<tr>
<td colspan="3">
<div>
<%
int i=0;
int cPage=0;
if(totalRows!=0) {
cPage=((int)(Math.ceil((double)endResult/(totalRecords*showRows))));
int prePageNo=(cPage*totalRecords)-((totalRecords-1)+totalRecords);
if((cPage*totalRecords)-(totalRecords)>0){
%>
<a href="pagination.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a>
<%
}
for(i=((cPage*totalRecords)-(totalRecords-1));i<=(cPage*totalRecords);i++){
if(i==((iPageNo/showRows)+1)){%>
<a href="pagination.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
<%
}
else if(i<=totalPages){
%>
<a href="pagination.jsp?iPageNo=<%=i%>"><%=i%></a>
<%
}
}
if(totalPages>totalRecords && i<totalPages){
%>
<a href="pagination.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >> Next</a>
<%
}
}
%>
<b>Rows <%=startResult%>-<%=endResult%>Total Rows<%=totalRows%> </b>
</div>
</td>
</tr>
</table>
<input type="submit" value="Update">
</form>
</body>
</html>
<%
try{
if(ps1!=null){
ps1.close();
}
if(rs1!=null){
rs1.close();
}

if(ps2!=null){
ps2.close();
}
if(rs2!=null){
rs2.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>

update.jsp:

<%@page import="java.sql.*"%>
<%
String d[] =request.getParameterValues("roll");
String name[] =request.getParameterValues("name");

String marks[] =request.getParameterValues("marks");

String lotNo[] =request.getParameterValues("lotNo");

%>
<%
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");

Statement st=null;
st=conn.createStatement();
for(int a=0;a<10;a++){
String no=d[a];
String tit=name[a];
String mar=marks[a];
String lot=lotNo[a];
st.executeUpdate("update student set rollNo='"+no+"', name='"+tit+"',marks='"+mar+"',grade='"+gra+"',lotNo='"+lot+"' where rollNo='"+no+"'");
}
}
catch(Exception e){
e.printStackTrace();
}
response.sendRedirect("pagination.jsp");
%>

Thanks

Javascript random number - Design concepts & design patterns
Javascript random number  Dear Sir, I have a table with many... are retreived from database.I have to generate random numbers corresponding to every row.But problem is one constraint is there while generating random numbers.If
Javascript random number - Design concepts & design patterns
Javascript random number  Dear Sir, I have a table with many... are retreived from database.I have to generate random numbers corresponding to every row.But problem is one constraint is there while generating random numbers.If
random number
random number   Please How do I generate a program that gives me random integer from 115 to 250? Using java.util.random. Thank you very much!  ... main(String[] args){ int min = 115; int max = 250; Random random
random number
random number  Sir could u please send me the code registration form of roseindia.net with image varification
using random number
using random number  generate a 10 digit number using random number and display the length of longest increasing series
getting random number in java
getting random number in java  getting random number in java Hi everyone, i am trying to get a random number in a particular range. for example... rand = new Random(); also created the random number genrator and translating
using random number
using random number  generate 10 number and display the length of longest incresing series
using random number
using random number  generate 10 digit number and display the length of longest incresing swries
Get Random Number
Get Random Number       Random Number is the set of unordered arranged number. The class ... to describe you a code that helps you in understanding  to Get Random Number
J2ME Random Number
J2ME Random Number       In this application we are going to generate the random number using Random... this random number generator's sequence and the setSeed method is used to sets
PHP Random Number
PHP Generate Random Numbers: A random number is becoming more useful.... To generate random number, PHP provides rand() function. ... will generate a random number as follows:</b>".rand(); echo "<br/>
Random Number Generation - Java Beginners
Random Number Generation  Can any one tell me about to generate integers that should be a 10 numbered integer. Should be between 100000000 to 9999999999. Core Application  Hi friend, Code to solve the problem
Number conversion in JavaScript
Number conversion in JavaScript  How do you convert numbers between different bases in JavaScript
How do I generate random number?
How do I generate random number?  In my Java program there is a requirement of generating random numbers between two given numbers. How do I write code for generating the random number? Thanks   Hi
Generating Random Number
Generating Random Number     ... for your application. Random Number is the frequently used to generate numbers... how to create a random number and example provided here will help you easily
MySQL random number
MySQL random number       You may fall in the condition when you want to create random number for your... create a random number by the use of RAND() function of MySQL. There are two
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
javascript max number - Java Beginners
javascript max number  hi all, i m trying to find maximum number in javascript but its not working dont know why, and heres the code: var number,i,max; for(i=1;i<11;i++) { number=prompt("Enter
How to generate random number in java
How to generate random number in java In this section you will learn how to generate random number in java. Java API  provide a random class in java.util.Random package which generate random number within a range. The random
autogeneration of a random number and displaying it as a alert message to the user on clicking submit
autogeneration of a random number and displaying it as a alert message... addorder form i want to produce a random number for every order made which is refrence number for dat order and is primary key and this number will be generated
JavaScript array get elements at random
JavaScript array get elements at random       In this example of getting JavaScript array elements randomly we have printed the array elements by the random index position
JavaScript for decimal number comparisons - Java Beginners
JavaScript for decimal number comparisons  Hi, I need to compare a BigDecimal in my javascript. I need a have a validation the text field value should not exceed (15,2) (max value is : 9999999999999.99). I am not able
how to validate the telephone number without using jquery in html with javascript
how to validate the telephone number without using jquery in html with javascript  how to validate the telephone number without using jquery in html with javascript
String Number Operations in JavaScript
String Number Operations in JavaScript  ... of JavaScript and create your first JavaScript program.  What is String Number...;/head> </body> </html> JavaScript in Number
Generate random numbers in Java
to generate desired random number in Java program? In this tutorial I will teach you... class for generating the random number. Random class is an utility class...: Random(): This constructor is used to create an instance of random number
JavaScript Array of number
JavaScript Array of number     ... in understanding Java Script array number of elements. We are using JavaScript...;/head>   <body>   <script language="javascript
random numbers
random numbers  hi.. i am creating a website and on feedback form to authenticate user i want to generate a random number on label.if user types that number correctly in textbox then he is allowed to submit feedback otherwise
javascript
javascript  hi, I was actually working on to calculate the number of days between two dates of yyyy-mm-dd format using javascript, and when i click on button then number of days should be display in textbox
Mobile Number Validation Form in JavaScript
Mobile Number Validation Form in JavaScript In this example we have created a Mobile Number Validation Form that displays the Mobile Number, Phone... Number to 10 digits and Phone Number to 10 Digits. We have also used Java
JAVASCRIPT
JAVASCRIPT  I have one textbox and I want to validate that it must start with number(1-0). can anyone tell me a javascript for that ? thanks in advance
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
Javascript
this format xxx-xxx-xxxx, i want the code in javascript. as a function.   Javascript Phone Number Validation <html> <script> function..." method="post" onsubmit="javascript:return validate();"> Phone Number:<
Pick at Random
how to display one name. The problem is how do I get javascript to remove the entry from the array before selecting another name at random
JavaScript toPrecision method
JavaScript toPrecision method       In JavaScript, toPrecision() method is used to convert a number into the number with the number of digits as provided for the precision. Syntax:ADS
random numbers
random numbers  Hi I need some help with the code for the this program. This program should randomly choose a number between 1 and 1000... the computer has randomly chosen the number 759.] Guess my number! It's between 1 and 1000
JavaScript
JavaScript  clone JavaScript
Javascript calculate number of days between two dates
Javascript calculate number of days between two dates In this tutorial, you will learn how to calculate number of days between two dates.For this, you need.... The desired interval can then be determined by dividing that number
Random in jsp
and less than 1.0. The random number are generated automatically.ADS_TO_REPLACE_1... random number will be generated. The code of the program is given below:  ADS...Random in jsp
JavaScript Math.sin() method
JavaScript Math.sin() method       JavaScript's Math.sin() method returns mathematical sine of the number and it returns value between -1 to 1. Syntax:ADS_TO_REPLACE_1
javascript
javascript  how to create random question using javascript?   <html> <script> function displayQuestions() { document.getElementById("text").value=" "; var Questions = new Array(20
javascript
Javascript
Javascript  How validations are done using javascript ,with example? and interview questions on javASCRIPT
JavaScript
should use JavaScript? Thanks   Hi, JavaScript is scripting language that runs on browser. You have to embed JavaScript in HTML page. Please see JavaScript tutorial. Thanks
JavaScript
JavaScript  how to get full path of a file type in javascript
javascript
javascript  Hi deepak, how to write form validation on javascript
javascript
javascript  write a program to display implement about browsers using javascript
javaScript
javaScript  How to open a browser window that cannot be resized? (HTML + Javascript