# Random Number 2 « Number « Java Data Type Q&A

### 2. Choosing 'random' numbers from set...    coderanch.com

Thanks for the 'even' idea - that worked! This is an assignment, and apparently there is supposed to be a way to get these random number using only one line of code. So I got the evens now - how about the other ones? How do I get a random number out of the set 3, 5, 7, 9, 11? Obviously ...

### 3. Weighted Random Number?    coderanch.com

Is there a way to do a weighted Random Number? I need to generate a random number between 1 and 3 but I want 1 and 3 to come up most of the time and the number 2 to only come up occasionally. For example, numbers 1 and 3 would come up 80% of the time and 2 would come up ...

### 4. JAVA RAndom Numbers    coderanch.com

I have a program to write for a computer science class I am in involving random numbers in JAVA. It involves a drunk man on a bridge, who starts at the center, and has 5 steps to one end or the other, and can last for 100 steps before falling asleep. This is done 5 times. I used 2 clases and ...

### 7. Random number    coderanch.com

class A { public static void main(String args[]) { System.out.println((int)(Math.random()*6)+5); } } The above code will generate integer random numbers in the interval [5,10] both inclusive. Note that the random number in the interval [0,1) is multipled with 6 so we will get numbers in the range [0,6). Adding five to the value will bring the value to [5,11). As the ...

### 10. How to get a random number    coderanch.com

Hello,i ve made a class with a method to get integer random numbers.I used the Math.random class first to get a random number obviously and then the Math.ceil class to make the double number an integer.When i compile it it gives me an error "possible loss of precision".Found double,required int My code is: ---------------------------------------------- private int number1; public void setNumber1(int n1) ...

### 11. What's up with a random number like this being displayed 889193265?    coderanch.com

Hi to anyone who reads this I am trying to build 2 programs. The 1st program, called MailOrderWrite, is suppose to allow you to enter data and save it to a file called info.txt which seems to work fine as far as to my knowledge about Java programming. The 2nd program, called MailOrderRead, is suppose to grab the information from the ...

### 13. random number    coderanch.com

A more generic solution may be: import java.util.Random; class RandomTest { public static void main(String[] args) { RandomTest rt = new RandomTest(); for (int n = 1; n < 11; n++) { rt.showNext( n ); } } public void showNext( int number ){ Random r = new Random(); int random = r.nextInt( 2 * number ) - number; System.out.println("Random for n ...

### 14. Three random numbers not working    coderanch.com

hello everybody, this is my first post, although i have read this forum in the past. i have a slight problem with a random number generator that im sure is simple to fix. my program has 1 button and 3 text boxes. when i click the button, a random number between 1 and 5 should appear in each of the three ...

### 15. Random six digit numbers    coderanch.com

Guys, I have a little issue. Basicaly I want to creat random numbers that are six digits and integers. Now i have no problem in creating random numbers, i do the following int test = (int) Math.round(Math.random() * 999999); (I use the round method that returns in long format hence whole number) Now this gives me values like 52127 , xxxx, ...

### 16. Random 5-Digit number    coderanch.com

Sorry i took so long but i still cant really figure it out. What im trying to do is create a simple bank account, which later i will make more complex. If you dont have an account it will make a random 5-digit number that it will assign as your Account#. So it must be 10000 through 99999. I looked at ...

### 17. random number question    coderanch.com

I need create an unique number that is going to be used in my program. I was looking at the documentation for the java.lang.Math random() - and I wanted to know how it came up with the number it comes up with - does anyone know how it generates it's number? I need to make sure that there is no chance ...

### 18. Randomly generate a string of number?    coderanch.com

static String oneWay() { java.util.Random rand = new java.util.Random(); StringBuilder buf = new StringBuilder(); for (int i = 0; i < 6; i++) { buf.append(rand.nextInt(10)); } return buf.toString(); } static String anotherWay() { java.util.Random rand = new java.util.Random(); int result = rand.nextInt(1000000); return String.format("%1\$06d", result); } // I'm sure there are plenty others...

### 19. Random numbers    coderanch.com

I need to ask the user for a numer between 1 and 1000. The computer should pick a random number. It needs to say if the user's guess is higher or lower than the random number. Here is my code: //Stephanie Dears //8 November 2005 //CMIS 241 import java.util.Random; import java.io.*; public class GuessNumber { public static void main(String[] args) { ...

### 20. random numbers    coderanch.com

If you make two Random-objects with the same seed, they will return the same values. Random r1 = new Random(0), r2 = new Random(0); for(int i = 0; i < 100; i++) { System.out.println(r1.nextInt() == r2.nextInt()); } That code would print 100 times 'true'. But if you make the objects with no parameters, it will print false. This is what API ...

### 21. random number generating    coderanch.com

Hi there can anyone help? I am attempting to generate 5 random numbers between 1 and 20 without duplicates. So far I have the code ARRAY_SIZE = 5; int[]random = new int [ARRAY_SIZE]; for (int x = 0; x < random.length; x++) random[x]=Math.abs(numGenerator.nextInt(20)) + 1; therfore, i can generate the numbers but do not know how to prevent duplicates. Would appreciate ...

### 22. Random number generation    coderanch.com

well, if your wanting to create a random number between 1-13(13 cards in each suit). u use a simple random generator, but you would have to keep track of the random numbers generated, as to not have doubles. or if your wanting to just load the arrays, with numbers 1 - 13, you could use a for loop. like Ankur said, ...

### 23. Random Number    coderanch.com

As shown above, the method getNextWhatever() is an instance method, meaning you have to have an instance of the MyClass class to call the method. The static keyword makes a class method, meaning you only have to have the class to call the method. The example changes to look like: public class MyClass { public static int getNextRandomFromOneToFive() { return ((new ...

### 24. Question about Random Number Generation...    coderanch.com

Hi, I need to generate 10 random numbers without repeating the same number again. This is what I need to achieve: I have a set of 100 questions and each time the test taker wants to take a test, the program should generate 10 questions from the available 100 question set. So, far i am able to generate random numbers without ...

### 25. Random Numbers    coderanch.com

When you say "get" here, exactly what do you mean? Because the nextInt() method just returns an int value. That's just a number. It isn't "in hex" or "in decimal" or "in binary" or anything. Those descriptions only apply when you choose to display that number in text form to the user (in this case you). So you must be asking ...

### 26. randomly picking numbers    coderanch.com

hi i'm stuck in a situation, i want to pick 10 random elements at a time from a vector.what i have done so far is, i generate a random number uses the generated random number as the index of the element be picked the element from the vector. Now my problem i don't want to pick the element which has already ...

### 27. RANDOM NUMBERS    coderanch.com

Isn't that as simple as taking the result of Math.random() and subtracting 0.5? If you care about the exact boundaries, there might be a problem. Math.random() gives a number greater than or equal to 0.0 and less than 1.0. So Math.random() - 0.5 gives a number great than or equal to -0.5 and less than 0.5. Is that close enough, for ...

### 28. random numbers    coderanch.com

you can't do directly from what is available in the API, but with a little thought and arithmetic you might come up with a way to do this. I am curious what problem solving steps you attempted, since this is an easy problem. Problem solving is very important, the API can't do all your thinking for you. If all you needed ...

### 29. how to generate random numbers ? Random class always generate same number....    coderanch.com

Originally posted by raminaa niilian: Hi Thank you for reading my post i need to generate some random numbers in my java program I tried to use but it always generate the same number , it never give me a new number . for example i need to generate 80 random number but it return 80 same number in its 80 ...

### 30. Random numbers    coderanch.com

Hello everyone,i'm having problem to generate random numbers within a certain range.I am using the following code: computersNumber = (int)(100 * Math.random()) + 1; The prob with this code is that it returns an integer between 1 and 100. I would like one that generate within a range for example 50 and 60. Hope to get some help. Thank you for ...

### 31. simple random number generator    coderanch.com

Hi, we forgot to say: Welcome to the ranch! If you're making a number generator for an assignment or just to do it, Google for algorithms. The good ones are not simple! If you need one for any kind of professional or serious use, try the library methods mentioned above. They're much better (more random) than anything we could make up. ...

### 33. random numbers gen    coderanch.com

By without repetition you mean you want to randomly permute a sequence of numbers and then show them one by one? If that's the case I would recommend creating a list of numbers from 1 to n and then calling Collections.shuffle on it. Then your "ith" random number will be the "ith" element of this sequence.

### 37. Random number without dublicate    coderanch.com

[Campbell]: That is a contradiction in terms; randomness implies the possibility of duplicates. Not at all. There are many types of random distributions. In this case, it's an even distribution across all numbers in a given range that have not previously been selected. That's fine - the fact that it's not an even distribution across all numbers in the range, period, ...

### 40. Help with Random Numbers plus    coderanch.com

Hi everyone. I'm am new....I am stuck...please help I'm trying to yes...do an assignment, but can't seem to wrap my head around it for some reason. The assignment is: -Create a lottery game application. -Generate three random numbers from 0-9 -Allow the user to guess three numbers. -Compare each of the users guesses to the three random numbers and display a ...

### 41. print out random number and state odd/even    coderanch.com

i have tried the following but got error message and can't proceed. <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page import="java.util.*"%> <% Random r=new Random(); int num = "r.nextInt(12)"; System.out.println(num); int pno=num-1; int nno=num+1; %> Lucky Number Advisor ...

### 42. How can I... make a random number of arguments call?    coderanch.com

(Whoa, that's what 'random' means, like in 'random acces') It's not really important (maybe). I'm just learning, and I'm a begginer, and I haven't got to the reflection yet, but I just have ideas , and I wonder if this is possible at all, maybe even how? So... can I call a method programmatically? I think that probably I can, with ...

### 43. Math.random().....what is the starting range ad ending range?can we customise the range numbers ?    coderanch.com

Math.random() randomly generates values of type double between 0.0000 to 1.000 including the boundary values. now using this you can generate all randome values by using an appropriate multiplier like 5.0 * Math.random() would generate random values between 0.00 to 5.00 300 * Math.random() would generate random values between 0.00 to 300.00 its very simple, i dont know why would you ...

### 44. Random Number Generation and Seed Value    coderanch.com

Hi, I am using the following code public static void main(String[] args) { // TODO Auto-generated method stub Random random1 = new Random(3); System.out.print("From random1: "); for (int i = 0; i < 10; i++) System.out.print(random1.nextInt(1000) + " "); Random random2 = new Random(4); System.out.print("\nFrom random2: "); for (int i = 0; i < 10; i++) System.out.print(random2.nextInt(1000) + " "); and ...

### 45. Generate Random Number within a range including negatives?    coderanch.com

In general, for any function f that returns a value between a and b, to get a value between c and d apply the following calculation: (f(x) - a) / (b - a) * (d - c) + c. The subtraction of a gives you a value between 0 and (b - a). The dividing by (b - a) gives you ...

### 46. Random numbers    coderanch.com

For 0 to 100 I use: combination = (int)(Math.random()*100)+1; As i know, Math.random generates a random number from 0 to 1, so if I want to generate a random nubmer to 100, I multiply that with 100 and then I add 1, beacuse I don't want to have 0 as my random number.

### 47. Create Random Number    coderanch.com

The Random class uses a pseudo random number algorithm to generate a series of numbers that approximate random numbers -- it is pure mathematics. There is nothing random about these numbers; given the same starting number (seed) it will generate the same exact series of numbers, every time. It is completely deterministic. By default, it uses the current time as the ...

### 48. How can I fix the size of randomly generated numbers ?    coderanch.com

Hi, I am randomly generating numbers using java.util.Random . But, I can not keep the length of the numbers fixed. Can you help me please ? Here's my code : package testShounak; import java.util.Random; /** * @author Shounak * */ public class RandomNum { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Random ...

### 49. Random Number Generation    coderanch.com

Hi I am reading Deitel book on Java package java.util.Random In chapter 6.9 it says that : "" The calculation that produces the pseudorandom numbers uses the time of day as a seed value to change the sequence's starting point. Each new Random object seeds itself with a value based on the computer system's clock at the time the object is ...

### 51. Random Number Generator    coderanch.com

I am having problems storing floating point numbers from a random number generator. I can store it once but its when i have to iterate it that the problem comes in. public static void main(String args[]){ Random generator = new Random(); // For 10 Reptitions for (int n = 1 ; n <= 10; n++){ double random = generator.nextDouble(); trunacation(random); }// ...

### 52. Beginner in java, need to write a random number generator.    go4expert.com

Hello everyone, I am new to java and am having quite a bit of trouble with it. I have to write a random number generator with numbers between 0-50. I also have to put something in with a determineLowest to return the lowest number. I am using Eclpise to write and test the program. Can anyone offer me some pointers or ...

### 56. Random number generator (I'm new to java)    go4expert.com

hai, I need to generate a 12-digit random per application such that the number generated should not be generated until the application is run for 4294967296 times. I need an approach to store the generated number in a file and modify the file after the file length reaches 4294967296. So please help me out ......

### 57. Random number generation    java-forums.org

So a random number from 0 to 8999999999999999 (8 + 15 9's) plus 1000000000000000 (1 + 15 0's) will give you what you want, right? So, check out the Random class and it's nextLong() method. Then, before adding 1000000000000000 (actually , make sure that the returned long is smaller than 8999999999999999 (8 + 15 9's) and at least 0 (although you ...

### 58. Trouble with Random number generation    java-forums.org

I need to create an array of 1000 random doubles. These doubles must be between 1.00 and 1,000,000.00. Here is my code for generating random numbers into the array without regard to the range of the values. I know how to range the values but i'm getting an error in the following code. double[] integers = new double[1000]; for( i = ...

### 60. Java random number    java-forums.org

Hey everyone, I am a beginner at Java ( totally ). And i wanted to create my first java program with the little things I know that will generate a random number in a defined domain I choose. ( eg a random number between [x,y] ) But something is wrong and I dont know why and where to look for a ...

### 61. print random numbers without repetition    java-forums.org

hii..i want to print 6 numbers randomly...the program for dat is below: //to print 6 random numbers in the range 1 to 49 public class PrrintRandom { public static void main(String[] args) { int i; for (i=0;i<=6;i++) { int n=(int)(Math.random()*49)+1; System.out.println(n); } } } above code is running fine...but i m getting duplicate numbers so the new code i wrote is ...

### 62. Powerball Example (Generating Random Numbers)    java-forums.org

I have to do an assignment where I have to write a program that simulates the selection of Powerball lottery numbers. I have to use the Math.random() function to simulate the random selection of a numbered ball. There has to be five white balls and one red ball. The five white numbers must be unique and between 1 and 49. (After ...

### 64. Generating random numbers    java-forums.org

Good day people,pls i need help developing a subroutine of function that generates 15 random integer numbers between 0 AND 75,but the caught is that once a number has been generated ,e.g 2 ,that number is never repeated or generated again,if it is the subroutine calls math.random until it generates a number that hasnt been generated before.How do i do this,do ...

### 65. Can I get random.util not to generate the same number twice    java-forums.org

Hi! I'm very new to Java and I'm trying to learn. I wanted to do a simple project where I use random to generate 4 random numbers between 1 and 35. These are my two methods: public void Lotto1(){ Message1("Lotto1"); Random randomGenerator = new Random(); for (int i = 1; i <= 4; ++i){ int randomInt = randomGenerator.nextInt(35); Message1(" " + ...

### 66. Random number generator    java-forums.org

I'm building a throw dice procedure for which I copied the code exactly from a popular online tutorial (you have heard of it). I can import the lib, import java.util.Random;, and finish the rest of the code and I get an error. Something like, Random can't be used here plus a lot of junk pointing to variable.nextInt (6);. I'm guessing the ...

### 67. Is Random() Only For Numbers?    java-forums.org

I have just recently begun to play around with the "Random()" utility in Java, though I have known about it for awhile now. All to tutorials I have followed the taught about random() dealt with numbers. What I am trying to build is a random quote generator which involves arrays and strings. Basically what I'm asking is: is there a way ...

### 68. Random Number and Table    java-forums.org

so im just getting started: I am trying to get a random number generator to generate either a 1,2,3 and then depending on the number put the word "car" into that cell of a table. I then ideally would want the other two empty cells to automatically be filled with a term such as "empty" this is what i have so ...

### 69. Random Numbers - finding least value    java-forums.org

Very, very new to Java and :confused:..Have code which generates random array of 100 numbers 0-9. It displays random numbers and also count of each random numbers. Last method which I am having major problem "should find least frequent value" and display it based on System.out.print. Been working on this over a week and now when I look at the code ...

### 70. Help with Random Number Generator    java-forums.org

Hey guys I need to make a program that is a guessing game. The user enters a number from 1 to 20 and my program tries to guess it using its previous guesses as parameters in a random number generator. The problem I'm having is how to generate a random number from the first guess to the number 20 (look at ...

### 71. Generate random number with specific ciphers    java-forums.org

Aha, ok, but I have also another problem... I don't know the exactly the number of cyphers that the random will have; I mean, in the code that I'm making, when you run the program it ask you how many cyphers do you want your random number to have? This also I don't know how to make it... But I have ...

### 72. Generate weighted random numbers, nextGaussian ()    java-forums.org

I would like to randomly pick an element from an array. I can create a random number in a range: Min + (int)(Math.random() * ((Max - Min) + 1)); but I want to weight the random numbers to have more lower numbers. The random function nextGaussian() generates a random number with normal distribution (which is what I want), but the upper ...

### 73. Random Lottery Numbers    java-forums.org

Is there some reason why you want to generate and return all 500 sets at once rather than calling the method 500 times and doing whatever it is you are doing with the results one at a time? If you *do* want 500 draws returned then Solarsonic is right: make lotteryNumbers a double array and use nested for loops to populate ...

### 75. checking min of four random numbers    java-forums.org

import java.util.Random; public class MinimumOfFourRandomNumbers { public static void main(String args[]){ Random random = new Random(); int num1 = random.nextInt(10); int num2 = random.nextInt(10); int num3 = random.nextInt(10); int num4 = random.nextInt(10); System.out.println(num1); System.out.println(num2); System.out.println(num3); System.out.println(num4); int x = Math.min(num1,num2); int y = Math.min(num3,num4); System.out.println("the minimum of num1 ,num2,num3 and num4 is" + Math.min(x,y) ); } }

### 76. Help with my first program. Random number gen.    java-forums.org

Ok this is what I am trying to do right now it make a set of random numbers. Kinda like a lotto picker. Two groups one for the first five numbers. One for the second group. I have that all good and well. But the issue is.. Some times they repeat. I'm guessing I need to stick a boolean in there? ...

### 77. help with math class and random numbers    java-forums.org

Java's Math class contains a static method called random. The RootTwo class below computes an approximation to the the square root of 2. However, there's a missing statement in the code. In the answer box provided, assign an appropriate random value to the variable val so that the code will calculate the intended approximation. Hint: the algorithm works this way: the ...

### 79. Adding individual numbers together from a random number    java-forums.org

No, this won't work, because if you cast a char to an int, you get the ASCII value of the char, not the numeric value of the char (assuming the char even is numeric). If you subtract '0' from each char, then it would work, but JosAH's solution is much more elegant anyway.

### 80. Random number generator with range.    java-forums.org

Hi Really struggling with this! As part of an assignment for uni I have been given a code that produces a random number. I have been asked to amende the code so that it print a random integer in the range 1 to 1000. The code I have been given is: import java.util.Random; public class RandomSample { public static void main(String ...

### 81. How to generate random numbers in a range?    forums.oracle.com

Obviously you didn't read this. It says to get a number between 0 and (max - min) which is actually what you did. But after that you need to add min. The first step would get a number in between 0 and 5. When you add 20 you'd get a number between 0+20=20 and 5+20=25.

### 82. problem with "Random number genrator witout repeat" code    forums.oracle.com

@chaos: Consider boolean rather than Boolean. As soon as check() has a (negative) result it can return since any subsequent checking wont ever make flag true. You are checking after you have filled and if there's a duplicate you start all over again. You could check after each array element is added: ie move the while loop inside the for loop. ...

### 84. Figuring out the occurence of random numbers?    forums.oracle.com

Flounder, I ruled something like that out of the question... since its in a loop... i thought it would repeat the statement over and over again.. Again, let me say what I'm having trouble with: After the program has generated 20 numbers, the program must be able to identify how many of each number occurred in sentence format.

### 86. Adding two random generated numbers together?    forums.oracle.com

You can't call the variable "final" as that's a Java keyword. Try something like "sum" or "total". (Also consider just using a single Random object and calling it twice to obtain the value for each of the dice.. And don't (cast) for the fun of it or as a talisman to ward off compiler messages.)

### 87. Help with Random Numbers + other Stuff...    forums.oracle.com

Hey I'm new here so be nice... I have an assignment which is to create a Game similar to Mastermind using only the Terminal I/O... Rules are pretty simple ... A string is generated eg. "ABC" and then the user has to guess the sequence correctly, after their guess they'll recieve feedback telling them how close they are to the correct ...

### 89. random numbers    forums.oracle.com

i need to generate a random number. I know i can use the class Random to do this, but i was wondering... within this class, i can generate a random int, long, float etc. I can specify the bounds for a random int, but i cannot specify the bounds for a random long... which is what i need. why is this? ...

### 93. random number generator without random class.    forums.oracle.com

First of all, just giving you the code would be cheating, and secondly, it's not like there's some single standard "here's how you do something like this" block of code that would necessarily be applicable. The standard thing to do would be to use the tool provided in the JDK. Beyond that, how one implements random number generators depends on the ...

### 95. Generating random numbers, while excluding some numbers    forums.oracle.com

if (number == 10) { System.out.println( " Piece made it home successfully"); numWins++; } } } System.out.println(" Number of times piece made it home successfully " + numWins); } } I want to be able to make sure that no number (number, number2, number3, or number 4) will ever be 6 or 9 Is there a way I can apply your ...

### 97. Generate random numbers with nextBoolean(), nextDouble(), and nexInt()    forums.oracle.com

randomNumber.java:18: not a statement boolean = myGen.nextBoolean(); ^ randomNumber.java:18: ';' expected boolean = myGen.nextBoolean(); ^ randomNumber.java:23: 'class' or 'interface' expected } ^ randomNumber.java:26: 'class' or 'interface' expected ^ randomNumber.java:1: cannot resolve symbol symbol : class Random location: package until import java.until.Random; ^ randomNumber.java:8: cannot resolve symbol symbol : class Random location: class randomNumber Random myGen = newRandom(); ^ randomNumber.java:8: cannot resolve ...

### 98. Help with Random Numbers..    forums.oracle.com

I need to create a class that will post a dialog box that lets the user input a number up to 100 and then the program will roll two dice that number of times in a random order. the problem is that I chave created the program adn it compiles but when it runs it does not generate any numbers, and ...

### 99. issuing random numbers    forums.oracle.com

Or you could check out the java.util.Bag because this structure will allow you to "grab()" things out of the bag in random order. Be careful because when you use this method is only returns a reference to the object in the bag so it's still there...Let me know if you're allowed or want to use this approach.

### 100. randomly generated numbers    forums.oracle.com

I?m familiar with the method of randomly generation a number between 1 and 10, But I need to know how to randomly generate a number between 1 and 10 only once during the duration that I run my programme, i.e. if 5 is randomly generated it can?t ever be generated again during the current run. once all numbers have been generated ...