| | |
generating random numbers into an array..
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Solved Threads: 0
Hi
I have searched this site for help with this and used an example of a lottery numbers program.
However, I need to modify it slightly but can't seem to get it right.
I would like to be able to generate random numbers between 0 and 100 (maybe negative oness as well). The code below just generates 7 numbers between 0 and 6 and I can't see where I need to adjust the code. If anyone could help me with this, I would be really grateful, even just an explaination so I can work it out myself would be great as I just can't see where it is saying only use those low 0 - 6 digits! :rolleyes:
// random number generator
import java.lang.Math;
// setting up array with random numbers 26 july 2005
public class myRandomNodes {
public static void main(String[] args)
{
new ranNumbers (7);
}
}
class myRanNumbers {
private int[] numbers;
public myRanNumbers (int n )
{
numbers = new int[n];
// initialise numbers
int i = 0;
while (i < n ) {
//int r = (int) (Math.random() * n);
// create a random integer between 1 and 100 inclusive
int r = (int) Math.floor(Math.random() * 100);
if (add (numbers, i, r)) {
++i;
}
}
show_all (numbers);
}
private boolean add (int [] list, int size, int val)
{
for (int i = 0; i < size; i++ ){
if (list[i] == val) {
return false;
}
}
list[size] = val;
return true;
}
private void show_all (int[] list)
{
for (int i = 0; i < list.length; i++ ) {
//System.out.print(list[i] + " ");
System.out.println("rounded number between 0 and 100 is ");
}
System.out.println();
}
}
I have searched this site for help with this and used an example of a lottery numbers program.
However, I need to modify it slightly but can't seem to get it right.
I would like to be able to generate random numbers between 0 and 100 (maybe negative oness as well). The code below just generates 7 numbers between 0 and 6 and I can't see where I need to adjust the code. If anyone could help me with this, I would be really grateful, even just an explaination so I can work it out myself would be great as I just can't see where it is saying only use those low 0 - 6 digits! :rolleyes:
// random number generator
import java.lang.Math;
// setting up array with random numbers 26 july 2005
public class myRandomNodes {
public static void main(String[] args)
{
new ranNumbers (7);
}
}
class myRanNumbers {
private int[] numbers;
public myRanNumbers (int n )
{
numbers = new int[n];
// initialise numbers
int i = 0;
while (i < n ) {
//int r = (int) (Math.random() * n);
// create a random integer between 1 and 100 inclusive
int r = (int) Math.floor(Math.random() * 100);
if (add (numbers, i, r)) {
++i;
}
}
show_all (numbers);
}
private boolean add (int [] list, int size, int val)
{
for (int i = 0; i < size; i++ ){
if (list[i] == val) {
return false;
}
}
list[size] = val;
return true;
}
private void show_all (int[] list)
{
for (int i = 0; i < list.length; i++ ) {
//System.out.print(list[i] + " ");
System.out.println("rounded number between 0 and 100 is ");
}
System.out.println();
}
}
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
// random number generator import java.lang.Math; public class myRandomNodes { public static void main(String[] args) { new ranNumbers (7); } } class myRanNumbers { private int[] numbers; public myRanNumbers (int n ) { numbers = new int[n]; // initialise numbers int i = 0; while (i < n ) { int r = (int) Math.floor(Math.random() * 100); if (add (numbers, i, r)) { ++i; } } show_all (numbers); } private boolean add (int [] list, int size, int val) { for (int i = 0; i < size; i++ ) { if (list[i] == val) { return false; } } list[size] = val; return true; } private void show_all (int[] list) { for (int i = 0; i < list.length; i++ ) { System.out.print(list[i] + " "); } System.out.println(); } }
Hope it makes it a bit easier to read - sorry about the unformatting before :-|
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Use the Random Class. It's more random than Math.random();
Be sure to add 1 if you want to reach 100.
Java Syntax (Toggle Plain Text)
import java.util.*; public class RandomClassTest { public static void main(String[] args) { Random randNumGenerator = new Random(); int[] x = new int[7]; for (int i=0; i<x.length; i++) { x[i] = (randNumGenerator.nextInt(100)+1); } } }
Be sure to add 1 if you want to reach 100.
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
Hi everyone,
Could not have said it better myself
Richard West
•
•
•
•
Originally Posted by server_crash
Use the Random Class. It's more random than Math.random();
Java Syntax (Toggle Plain Text)
import java.util.*; public class RandomClassTest { public static void main(String[] args) { Random randNumGenerator = new Random(); int[] x = new int[7]; for (int i=0; i<x.length; i++) { x[i] = (randNumGenerator.nextInt(100)+1); } } }
Be sure to add 1 if you want to reach 100.
Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Random Numbers (C++)
- Logic behind generating random number (C)
- not getting non repeating random numbers many times (C)
- C++ Reorder random numbers (C++)
Other Threads in the Java Forum
- Previous Thread: Getting Cmos Date
- Next Thread: Array
Views: 10766 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
account android api apple applet application arguments array arrays automation binary bluetooth chat class classes client code columns component data database draw eclipse error event exception expand file filechooser fractal game givemetehcodez google graphics gui helpwithhomework homework html ide image inheritance input integer j2me java javaprojects jlabel jme jmf jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception number object open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms socket sort sourcelabs splash sql sqlite static string swing test testautomation threads time transfer tree windows






