RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5104 | Replies: 5
Reply
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

generating random numbers into an array..

  #1  
Jul 28th, 2005
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();
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: generating random numbers into an array..

  #2  
Jul 28th, 2005
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: generating random numbers into an array..

  #3  
Jul 28th, 2005
// 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 :-|
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: generating random numbers into an array..

  #4  
Jul 28th, 2005
Use the Random Class. It's more random than Math.random();

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.
Reply With Quote  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Solution Re: generating random numbers into an array..

  #5  
Jul 29th, 2005
Hi everyone,

Originally Posted by server_crash
Use the Random Class. It's more random than Math.random();

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.

Could not have said it better myself

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
Reply With Quote  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: generating random numbers into an array..

  #6  
Jul 30th, 2005
Hi

Thanks very much for the response - appreciate it. :mrgreen:
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:10 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC