Generating Double Number From The Range
Apr 18, 2014
I am trying to generate a double number from the range [-1,2]
so what i did is this
xs[i]= Math.random() *(2-(-1))+(-1);
I did that as a loop to generate multiple numbers and i got this result:
initial x values 2.17 2.66 3.04 2.81 1.83 2.66 3.67 2.81 1.04 3.1 3 1.23 1.44 3.5 3.84 3.03 1.7 2.79 4 1.43
see some numbers are out of range! and i don't know why aren't there any minus numbers...
View Replies
ADVERTISEMENT
Mar 28, 2015
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h
My guess is too high?
My fifth guess is 16: h
*** I am unlucky this round. I give up.
Let's play!
My first guess is 10:
etc..
import java.util.*;
import java.lang.Math;
public class numguessprac1 {
// Declaring variables
public static String input;
public static int quit;
public static int guess;
public static int wins;
[Code] ....
View Replies
View Related
Oct 31, 2013
I am very new to programming. This is for a college assignment. It says in the brief of the assignment that we will need to convert Math.random to output a random number between 1-1000. How can I do this?
View Replies
View Related
Dec 8, 2014
The program I'm supposed to create generates a random number between one to ten. Then the program is to ask me if I wish to cross the road.
If you choose to cross, the outcomes for 0-2 are "You crossed safely."
For 3-5, 75% of the time it should say "RIP you got run over", and 35% of the time it should say "You crossed the street."
For 6-8, 60% of the time it should say you made it.", and 40% of the time it should say "You died". For 9-10, it should say "RIP".
So far I have gotten the random number generation part working,
import java.util.Random;
public class test4 {
public static void main(String[] args) {
Random random = new Random();
for(int i =0; i < 1; i++){
int num = random.nextInt(10) + 1;
System.out.println("The number of cars on the street are: " + num + "
Do you wish to cross the road?");
}
}
}
View Replies
View Related
Apr 23, 2014
Any way to shift in a range from 0-9 when I already have the shift value.
The reason I am asking this is because I am writing a telephone validation program and I got most of it complete and all I need to do now is the shift an encrypted phone number given to me by the user, and shift it however many times my shift value is.
Example: I am trying to get this phone number, 545-319-8712 to become 212-086-5489. The shift value is 3. So basically since the phone number given to me is 3 numbers higher than the phone number I am trying to get, so if the first number I receive from the user is higher than 2 then I would shift the number the user gave me down by the shift value I have already gotten.
5 shift down 3 = 2, 4 shift down 3 = 1, etc. But I also want to know how I can make a number like "1" to shift down 3 to become 8. This is the range; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
If I have to shift a number down 4 spots and I get the number 1 from the user than I want to get the number 1 to first down four times to become 7.
[1] -> 0 -> 9 -> 8 ->[7]
Basically if I have to shift a number down 4 and the number is less than or equal to 3 then I want it to continue from 9 .
Then just reverse the steps if I have to shift a number up, USER gives me "090", i want "212" I shift the number up by 2.
View Replies
View Related
Feb 5, 2015
public class printprimes2{
public static void main(String[] args){
for( int i = 1 ; i <=199 ; i++ ) //iterate 1 - 199; 2 is prime {
for ( int j = 2 ; j < i ; j++ ) //iterate 2 - potential composite EXCLUSIVELY; every number can be divided by one and itself
[Code] ....
It doesn't print only prime numbers but all numbers that range from 0 to 199. What do you think I am doing wrong?
View Replies
View Related
Jul 28, 2014
For example:
if(JTextField = 15-30){
do this
}
I know it's simple but i have no clue how it's done....
View Replies
View Related
Feb 13, 2015
The assignment is to make a program that prints the number of prime numbers in a range. This is what i have so far. The output is a list of 2s. I created the for loop to cycle through the range of 17-53 and nested a while loop within to test each incident of the for loop to check for divisors starting with 2 until the modulus result is 0 resulting in a false for being a prime number. Then the loop should increment to the next i value. The last part is an if statement that i had intended to add counters to the k variable that would keep track of the number of prime numbers.
boolean isPrime = true;
int j = 2;
int k = 1;
for (int i = 17; i <= 53; i++){
{
while (i % j == 0){
isPrime = false;
[Code] .....
View Replies
View Related
Oct 31, 2014
I found an exercise online to create a small program . I have this code that I have done so far:
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong(); long b = sc.nextLong();
long count = 0; // counter
for (long c = a; c <= b; c++) {
if (c % 2 == 0 || c % 3 == 0 || c % 5 == 0) {
count++;
}
}
System.out.println(count);
}
}
This program is suppose to give me the number of numbers which are dividable by either 2,3 or 5 in a range of a to b, where a<=b.
FOR EXAMPLE: a=5 b=8 ... output: 14 (since there are 14 numbers in between 5 and 8 which are dividable by either 2,3 or 5.)
It works great for all of the numbers except higher ones such as a=123456789012345678 b=876543210987654321. Here it doesn't give me any output. From what I know it is because the code is still running. But there must be a quicker way ...something that can modify the code so it finishes in the matter of seconds not hours. Something that will fasten the process of checking if the numbers are dividable...
View Replies
View Related
Apr 24, 2015
I am trying to write a Random Number Generator that will not contain any repeat value in a given range
something this I have try so far
import java.util.Random;
/**
* This class is used to generate a
* Range of Random Number in a range from int a to int b
*/
public class RandomNumberGenerator {
public static void main(String[] args) {
int[] arr=randomNummbers(1, 20);
for(int j=0;j<arr.length;j++){
[Code]...
although this code generate random numbers but some values are also getting duplicate.So how to write a program for random number that will not repeat any integer in range?
View Replies
View Related
Oct 29, 2014
I have a real number. example:
double number = 1.95842175;
I want to split it into 2 integers:
int a1 = 1;
and
it a2 = 95 (2 numbers after '.')
How?
View Replies
View Related
Apr 6, 2014
Where do I find a subroutine similar to this:
String formatDouble(double dblNum,int NumOfDecDigits) { }
For years I have been using output subroutines I wrote long ago.
Now I need this kind and can't remember where it is.
How do I print doubles to a fixed number of digits to right of the point?
View Replies
View Related
Nov 24, 2014
for (int i = 0; i < letters.length; i++) {
double relativeFreq = (onesAverage()/letters[i]);
char a = characters.charAt(i);
double j = score[i];
System.out.printf();//ToDo
Above is my current code. Basically- There is a string called characters of length 26 (1 char for each letter in alphabet), a double relativeFrequency value which does a calculation and the end result is a number with lots of decimal numbers. The score array has set double values within the array.
What I want to do is create a print f statement where I print out each of the values I initialized above WITH specific amounts of spaces in between. So it should be in this order:
a (spaces) j (spaces) relativeFreq (newLine).
Between J and relativeFreq there should be 4 spaces, however if the value in relativeFreq is greater than 10 (it won't be greater than 100)- then there should be 3 spaces. Everytime I try to create a printf statement my code ends up crooked for some of the values.
for (int i = 0; i < letters.length; i++) {
double relativeFreq = (onesAverage()/letters[i]);
char a = characters.charAt(i);
double j = score[i];
System.out.printf("%c %.4s %.4s%n",a, j, relativeFreq);
View Replies
View Related
Nov 11, 2014
I am writing a code in which a user inputs data as a string and that data must be verified as a valid number. A valid number is anything from 0-100. Then all valid numbers are converted into double numbers.
I am having trouble in how to write the validation part of the code.
Is it suppose to be an if, else statement? And if so how is it suppose to be validated?
View Replies
View Related
Nov 4, 2014
My random integer always seems to be zero.. I am at the ends of my wit.
package Exercises;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
* Heads or tails?
* That is what this is.
*/
public class num14 {
[Code] ....
Attached File(s) : New Text Document.txt (2.59K)
View Replies
View Related
Sep 5, 2004
Is it a good idea to use the date and time with the first or last few values of the session ID. Or should I just use the complete session ID value for my "unique id"?
View Replies
View Related
Jun 25, 2014
I want to call getter method from method based on parameter send to this method e.g
Java Code:
void myCrazyMethod(MyDto dto, String prop){
System.out.println("Value of " + prop + ": " + dto.get{Prop}()");
} mh_sh_highlight_all('java');
View Replies
View Related
Jun 8, 2014
My project is writing a program that generates a series of numbers to appear like a social security number (XXX-XX-XXXX). My code runs.. But any number below 10 it just shows one number (XXX-X-XXXX). What do I need to enter in to my code so that if the number is <10 it will show (00,01,02,03....)?
Java Code:
import java.util.Random;
public class IDnumber
{
public static void main (String[] args) {
Random generator = new Random()
int num1 = (generator.nextInt(7) + 1) * 100 + (generator.nextInt(8) * 10) + generator.nextInt(8);
int num2 = generator.nextInt(74);
int num3 = generator.nextInt(10000);
String IDnumber = num1 + "-" + num2 + "-" + num3;
System.out.println(IDnumber);
}
} mh_sh_highlight_all('java');
View Replies
View Related
Jun 25, 2014
I have a problem in generating reports using java code. I created a report using iReport 5.5.0 with MySql Database connection. In iReports, it shows the correct output while previewing it. Then i copied my .jrxml file and pasted it into my netbeans project folder(..sample/web/WEB-INF/sample.jrxml) and then i added the required jar files in my project's library folder. i am generating the report in netbeans using jsp and servlet. THE REPORT IS GENERATING AND SAVED SUCCESSFULLY IN MY DESKTOP. BUT IT DOESN'T GENERATE THE REPORT FOR FIRST RESULT. For Example.., if i generating report based on a particular date. After executing the query, it returns 4 results. So it has to create the reports in 4 pages. But it generates the report for last 3 pages only. It doesn't generate the report for first page. I have attached my .jrxml file servlet file for your reference.
View Replies
View Related
Nov 3, 2014
I have been set this task, which is supposed to make me code using string arrays. The idea is to generate random sentences.This is what i have been able to do so far :
package usingarraysagain;
public class sentences {
public static void main (String[] args){
String[] NOUNS = { "lizards",
"Nikola Tesla",
[code]....
View Replies
View Related
Sep 15, 2011
I am trying to change this code to use only integers to calculate the compound interest.
// Compound-interest calculations with for.
public class Interest {
public static void main( String args[] ) {
double amount; // amount on deposit at end of each year
double principal = 1000.0; // initial amount before interest
double rate = 0.05; // interest rate
[Code] .....
And here is the output I get :
Why do I get the output after year 2? I assume it has something to do with the remainder.
I also have to format this output with the decimal point, etc.. which I think I will be ok with after I get through this part.
View Replies
View Related
Jan 13, 2014
I've been trying to find the easiest way to write to generate a number which is between intervals of a arbitrary min and a max value.I've been searching for this but I don't find this particular thing.I've found that this combination works:
Java Code: int guess = rand.nextInt(max - (min - 1)) + min; mh_sh_highlight_all('java');
But I wonder, is this really the easiest way of writing it?
View Replies
View Related
Aug 21, 2014
I am working on a bingo project and created random numbers between 1 and 75 to generate. I have it set up that it shows the number in a text box and changes the color on the board if the number is called. When I test the program, some numbers are highlighted in addition to the number called. I believe this is because extra numbers are being chosen, but not being noted in the text box. Below is my code for the unique random numbers.
int CallNo;
String CallerTxt = new String();
public void CallNum() {
Random RandCall = new Random();
//Generate Caller Random Number
CallNo = RandCall.nextInt(75)+1;
[Code] ....
View Replies
View Related
May 10, 2014
After generating a. JAR with Netbeans Java, when I play I see the colors of the components, the design and formatting is lost and the form gets a very basic formatting, for example, if I set a button with the color [0, 40.255] and build the. JAR after this, when I run the. JAR this button turns gray, and it happens with all the layout of the form.
settings:
Netbeans 8.0
Windows 7
View Replies
View Related
Nov 10, 2014
I am trying to iterate through all combinations of characters given a length n with the added notion that all characters are numerical (0-9).e.g I have been given n = 5. The I want to iterate through all combinations starting with "00000" and ending with "99999". My first instinct was to just have a for loop like the following:
for(int i= 0; i<99999; i++){
// extra code here
}
but obviously this does not a account for combinations such as "00010".
View Replies
View Related
May 11, 2014
After generating a. JAR with Netbeans Java, when I play I see the colors of the components, the design and formatting is lost and the form gets a very basic formatting, for example, if I set a button with the color [0, 40.255] and build the. JAR after this, when I run the. JAR this button turns gray, and it happens with all the layout of the form.
settings:
Netbeans 8.0
Windows 7
View Replies
View Related