954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

generating random numbers into an array..

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();
}
}

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
// 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 :-|

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Hi everyone,

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

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

Hi

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

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You