Home Answers Viewqa Java-Beginners generating random numbers

 
 


tavvy
generating random numbers
1 Answer(s)      3 years ago
Posted in : Java Beginners

We would like to be able to predict tomorrow's price of a share of stock. We have data on past daily prices. Based on that we will make a prediction. Our plan is to use a weighted average of the 5 most recent prices as the prediction.
We will choose the appropriate weights as the ones that would have best predicted prices in the past. The weights must add up to one to be a weighted average, but some of them may be negative. We will restrict consideration to weights that are chosen from the following 21 values:
-1.0, -0.9, ... , -0.1, 0.0, 0.1, ..., 0.9, 1.0
We define the "error" of a prediction to be the absolute value of the difference between the prediction and the price.We will evaluate a possible weighting by using it to predict each of the known prices (except for the first 5, which don't have enough predecessors). We will then choose the weighting that has the smallest average error for its predictions.
Before we use our weighted averaging scheme to make our fortune on the stock market we need to have some idea of how well it predicted past data. Create a class Predicting that contains a method avgError that will be given a double[] data and will return the average error made by our best weighting.
Definition


Class: Predicting
Method: avgError
Parameters: double[]
Returns: double
Method signature: double avgError(double[] data)
(be sure your method is public)

Notes

- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints

- data will contain between 6 and 50 elements inclusive.
- Each element of data will be between 10.0 and 100.0 inclusive.
Examples

0)

{10,10,10,10,10,10}
Returns: 0.0
A weighting of .2,.2,.2,.2,.2 will exactly predict the only past price that had 5 predecessors.
1)

{50,10,50,10,50,10,50,10,50,10,50,10}
Returns: 0.0
A weighting of -1,0,0,1,1 predicts price correctly every time (in the past). For example, the prediction of the most recent price is -1*50 + 0*10 + 0*50 + 1*10 + 1*50 = 10 which was exactly right.
2)

{50,60,50,60,50,60,60}
Returns: 5.0
The best choice of weights is -1.0,-1.0,1.0,1.0,1.0 which gives a prediction of 50 for the next to the last price (-1*50 + -1*60 + 1*50 + 1*60 + 1*50 = 50) and a prediction of 60 for the last price (-1*60 + -1*50 + 1*60 + 1*50 + 1*60 = 60). So the errors are 10 and 0 for the two predictions with an average error of 5.
3)

{82.9102, 70.6848, 21.503, 61.4588, 54.7789,
48.9889, 57.6766, 91.1859, 26.3674, 55.4601,
53.9357, 87.2005, 78.4771, 65.0102, 18.619,
90.296, 26.3894, 53.8588, 91.8369, 58.8028,
74.0577, 28.2406, 65.609, 59.4867, 27.7544,
54.6992, 69.2428, 22.6264, 87.0083, 58.5116,
60.286, 20.4318, 65.6475, 11.8348, 36.3488,
92.8092, 60.7392, 98.124, 48.1292, 39.5459,
52.2657, 34.3519, 38.9279, 93.0152, 11.3157}
Returns: 22.0175905
View Answers

May 27, 2010 at 5:24 PM


Hi Friend,

Try the following code:

import java.util.*;
class Predicting{

public static double avgError(double []array){
double aa[]=new double[5];
for(int i=0;i<5;i++){
aa[i]=array[i];
}
double max=1.0;
double min= -1.0;
double ran[]=new double[5];
double error;
double sum=0,sum1=0;
for(int i=0;i<5;i++){
ran[i]=(double)(Math.random() * (max - min + 1) ) + min;
}
double[] arr=new double[5];
for(int i=0;i<arr.length;i++){
arr[i]=ran[i]*aa[i];
sum+=arr[i];
}
double last=0;
for(int i=0;i<array.length;i++){
last=array[array.length-1];
}

if(sum==last){
error=sum-last;

}
else {
shiftRight(array,1);
double aaa[]=new double[5];
for(int i=0;i<5;i++){
aaa[i]=array[i];
}

for(int i=0;i<arr.length;i++){
arr[i]=ran[i]*aaa[i];
sum1+=arr[i];
}
double e=last-sum;
double f=last-sum1;
double v=e+f;
error=v/2;

}
return error;

}
public static double[] shiftRight(double[] array, int amount) {
for (int j = 0; j < amount; j++) {
double a = array[array.length - 1];
int i;
for (i = array.length - 1; i > 0; i--)
array[i] = array[i - 1];
array[i] = a;
}
return array;
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter 10 values:");
double arr[]=new double[10];
for(int i=0;i<arr.length;i++){
arr[i]=input.nextDouble();
}
double err=Predicting.avgError(arr);
System.out.println(err);
}
}

Thanks









Related Pages:
generating random numbers - Java Beginners
generating random numbers  We would like to be able to predict tomorrow's price of a share of stock. We have data on past daily prices. Based on that we will make a prediction. Our plan is to use a weighted average of the 5 most
Generating Random Numbers to Fill array. Java Beginner needing help!
Generating Random Numbers to Fill array. Java Beginner needing help!  ... a program that produces random permutations of the numbers 1 to 10. eg... the permuted numbers. Make a second array and fill it with the numbers 1 to 10
Generating Random Number
Generating Random Number       In many places you need random numbers to fulfill your requirements. Java provides classes to help you in generation random numbers
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
Generate Random Numbers
Generate Random Numbers  hi,,, Please give me answer with example How do you generate random numbers within a given limit with actionscript... This function generate random numbers with in given limit
random numbers - Java Beginners
random numbers  write a program to accept 50 numbers and display 5 numbers randomly  Hi Friend, Try the following code: import...(); System.out.println("Random numbers are: "); int random[]=new int[5]; for(int i=0;i
Random numbers - Introduction
Java NotesRandom numbers - Introduction When to use random numbers There are many types of programs that use random numbers. Game programs use them... numbers. You might want to use random numbers to change the appearance
random numbers - Java Beginners
random numbers  Hello i need this code to be modified for me to display the random numbers, but not twice or more. I mean i need a number...("Random numbers are: "); int random[]=new int[5]; for(int i=0;i  Hi
Random numbers - Development process
Random numbers  hi, How to generate unique random numbers between range like(10 to 50)  Hi friend, class RandomNumber { public static void main(String[] args) { //set the variable aNumber
Random Numbers - shuffling
is described at the bottom. Shuffling: Random numbers without replacement The random number methods generate numbers with replacement. This means... numbers - intro Random numbers - API Copyleft 2005 Fred Swartz MIT
generate random numbers and display the largest
generate random numbers and display the largest  Hi, I am using... creates an array and fills it with random ints, prints the contents... ArrayRandom{ public static void main(String args[]){ Random r = new Random
Random numbers - API
class often produce random numbers in a more convenient form, but requires creating... directly useful numbers from the Random class. java.util.Random class To use... and standard deviation 1.0 Example: Generating a random Color To create any color
Random in jsp
Random in jsp          Random numbers are the numbers that are determined entirely by chance. User does not have any control over the working of random numbers
Javascript random number - Design concepts & design patterns
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... one group there will be 10 rows and their random numbers will be same.Then next
Generate array of random numbers
Generate array of random numbers You can generate a random number either...() but it is better to use Random class. If you want to generate a series of random numbers...(); } System.out.println(); System.out.println("Random numbers are: "); int random
Generate array of random numbers without repetition
Generate array of random numbers without repetition In the previous section, you have learnt how to generate an array of random numbers from the given array... created an application that will generate the array of random numbers without
Javascript random number - Design concepts & design patterns
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... group there will be 10 rows and their random numbers will be same.Then next 10
JavaScript array get elements at random
of the array. For generating random numbers we have used the Math.random() function... random numbers between value "0" and "9". Each and every time... JavaScript array get elements at random   
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 random numbers between 1 to 100 in Java. Though i m new to Java, i have tried many
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program to use a File object and print even numbers from two to ten. Then using RandomAccessFile write numbers from 1 to 5. Then using seek () print only the last 3 digits
switch Example - Random Insults
Java Notesswitch Example - Random Insults The following class could be useful for generating random insults in response to erroneous user input... the logic for generating the insult in a string. It knows nothing about the user
PHP Random Number
PHP Generate Random Numbers: A random number is becoming more useful.... To generate random number, PHP provides rand() function. ... Value A random value  Example : <
understanding buttons :JSP random no program
understanding buttons :JSP random no program  I hav java random... random no between 100 and 200 on loading and on clicking submit button it ives... "no is greater" please help me to understand buttons .thank you java code for random
MySQL random
and thereafter we can show the result of it. Numbers generated are in the random... MySQL random       In MySQL we can get the random records by using the method RAND() of MySQL
generating mock data class of times (start and end time and value)
generating mock data class of times (start and end time and value)   Using the timertask function want to generate a set of mock data of times using the random DATE class and values for plotting on a graph using java. How
generating itext pdf from java application - Java Beginners
generating itext pdf from java application  hi, Is there any method in page events of itext to remove page numbers from pdf generated frm java application.  Hi friend, Read for more information. http
MySQL random number
versions of RAND() function : first RAND() and second is RAND(X). Random numbers... is not a perfect random number but it is very close to the perfect random numbers. Here.... Numbers generated are in the random order which means that they lack any pattern
Default constructor generate two random equations
Default constructor generate two random equations  Need to create a default constructor in a programmer defined class that generates a random question, addition or subtraction. And when adding the numbers must be random from 0-12
Assigning ordinate numbers - JSP-Servlet
, For random number: import java.util.*; public class OrdinalNumber { public static...[] args) { OrdinalNumber number = new OrdinalNumber(); Random r=new Random
Randomizer
; In this example we are discussing how to generate the random numbers... random numbers by apply some mathematical operations onto them and display 10 random numbers on the console. Description of program:  In the given
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
random number
random number  Sir could u please send me the code registration form of roseindia.net with image varification
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!  ...[] args){ int min = 115; int max = 250; Random random = new Random
java programming:generating series
java programming:generating series  WAP to print series: | + || + ||| + |||| + .......... n terms
Generating report in java
Generating report in java  How can I generate a report in java
Generating pdf in Spring
Generating pdf in Spring  Sir/Madam, I need your help in generating a pdf by fetching the data form database and by using unicode in spring framework
Pick at Random
the entry from the array before selecting another name at random
Random Access File.
Random Access File.  Explain Random Access File
Java programming: generating series
Java programming: generating series  Write a program to accept a string using buffered reader and replace character sequence 'cat' with 'dog'   Here is a java code that accept the string from the user using
java programming:generating series
java programming:generating series  Write a program to generate series: 12345 1234 123 12 1 1 12 123 1234 12345 12345   Here is a code that displays the following pattern: 12345 1234 123 12 1 1 12 123 1234
java programming:generating series
java programming:generating series  12345 1234 123 12 1 1 12 123 1234 12345   Here is a code that displays the following pattern: 12345 1234 123 12 1 1 12 123 1234 12345 class Pattern { public
Generating dynamic fields in struts2
Generating dynamic fields in struts2  Hi, I want generate a web page which should have have some struts 2 tags in a group and a "[+]" button for the group. On click of this button one more group of fields should be generated
java programming:generating series
java programming:generating series  1234554321 1234 4321 123 321 12 21 1 1   Here is a java code that displays the following pattern 1234554321 1234 4321 123 321 12 21 1 1 class Pattern
using random number
using random number  generate a 10 digit number using random number and display the length of longest increasing series
Random Access File class
Random Access File class  Write a short note on Random Access File class.   Please visit the following link: Java Random Access File
Random data - Java Beginners
Random data  hi friend, I want to ask how to make random data. i has example word like this : Gaza! Gaza! Palestina merdeka! how to make random like : Azag! Azag! Anitselap akedrem! thank's
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  In a random access file, first write the alphabets a,b, c, d till z. Read the file in reverse order and print it on screen

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.