944,111 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 24958
  • Java RSS
Jul 28th, 2005
0

generating random numbers into an array..

Expand Post »
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();
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Jul 28th, 2005
0

Re: generating random numbers into an array..

1) use code tags
2) follow the Sun coding standards, which you can get from Sun.

Doing both will make your code a lot easier to read and debug, as it stands I'm not even going to try.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jul 28th, 2005
0

Re: generating random numbers into an array..

Java Syntax (Toggle Plain Text)
  1.  
  2. // random number generator
  3.  
  4. import java.lang.Math;
  5.  
  6.  
  7. public class myRandomNodes
  8. {
  9. public static void main(String[] args)
  10. {
  11. new ranNumbers (7);
  12. }
  13. }
  14.  
  15. class myRanNumbers
  16. {
  17. private int[] numbers;
  18.  
  19. public myRanNumbers (int n )
  20. {
  21. numbers = new int[n];
  22.  
  23. // initialise numbers
  24. int i = 0;
  25.  
  26. while (i < n )
  27. {
  28. int r = (int) Math.floor(Math.random() * 100);
  29. if (add (numbers, i, r))
  30. {
  31. ++i;
  32. }
  33. }
  34.  
  35. show_all (numbers);
  36. }
  37.  
  38. private boolean add (int [] list, int size, int val)
  39. {
  40. for (int i = 0; i < size; i++ )
  41. {
  42. if (list[i] == val)
  43. {
  44. return false;
  45. }
  46. }
  47. list[size] = val;
  48.  
  49. return true;
  50. }
  51.  
  52. private void show_all (int[] list)
  53. {
  54. for (int i = 0; i < list.length; i++ )
  55. {
  56. System.out.print(list[i] + " ");
  57. }
  58. System.out.println();
  59. }
  60. }


Hope it makes it a bit easier to read - sorry about the unformatting before :-|
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Jul 28th, 2005
0

Re: generating random numbers into an array..

Use the Random Class. It's more random than Math.random();

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class RandomClassTest
  4. {
  5. public static void main(String[] args)
  6. {
  7. Random randNumGenerator = new Random();
  8.  
  9. int[] x = new int[7];
  10. for (int i=0; i<x.length; i++)
  11. {
  12. x[i] = (randNumGenerator.nextInt(100)+1);
  13. }
  14. }
  15. }

Be sure to add 1 if you want to reach 100.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 29th, 2005
0

Re: generating random numbers into an array..

Hi everyone,

Quote originally posted by server_crash ...
Use the Random Class. It's more random than Math.random();

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class RandomClassTest
  4. {
  5. public static void main(String[] args)
  6. {
  7. Random randNumGenerator = new Random();
  8.  
  9. int[] x = new int[7];
  10. for (int i=0; i<x.length; i++)
  11. {
  12. x[i] = (randNumGenerator.nextInt(100)+1);
  13. }
  14. }
  15. }

Be sure to add 1 if you want to reach 100.
Could not have said it better myself

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Jul 30th, 2005
0

Re: generating random numbers into an array..

Hi

Thanks very much for the response - appreciate it. :mrgreen:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Getting Cmos Date
Next Thread in Java Forum Timeline: Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC