Results 1 to 9 of 9
  1. #1
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default random numbers without using java funtions

    i need a formula to generate 100 random numbers 1-20

    any suggestions?

    remember, no functions

    thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    int n = (int)(1 + 20*Math.random());

  3. #3
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    we cannot use the Math.random function

  4. #4
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    this is some code it - Each number needs to be generated.
    Each number should be generated at least 300 times of the 10,000.


    Java Code:
    public class Random_Gen
    {
        int min;                     //minimum number that can be generated.
    	int max;                    //maximum number that can be generated. 
        int value;
    
    	public Random_Gen(int n, int m)
    	{
            max = m;		//code goes in this class
    	    min = n;
    	   
    	}
    	
    
    	public int getRandom()
    	{
    	 
          
    	    return value;
    	    
    	 }
    }
    public static void main(String[] args)
    	{
    	    int min = 1;                 // smallest number that can be generated
    	    int max = 20;                // largest number that can be generated
    	    Random_Gen  rg = new Random_Gen(min, max);
    	    int num;
    	    int[] frequency = new int[max];
    	    
    	    
    	    for (int x = 0; x < 10000; x++) 
    	    {
    	       num = rg.getRandom();
    	       
    	       System.out.println(num);
    	       
    	       for (int index = 0; index < max; index ++)     //determines frequency
    	          {
    	           if (num == index + 1) 
    	             frequency[index]++;
    	           }
    	     }
    	     
    	     
    	      for (int index = 0; index < max; index ++)     // displays frequency
    	    {
    	        System.out.println(""+(index+1) + " = " + frequency[index]);
    	    }
       }

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Can you use the java.util.Random class?
    Edit: random numbers without using java funtions
    aha - no java. Check the next method in the Random class for some clues.
    Last edited by hardwired; 11-15-2007 at 05:01 AM. Reason: read the title

  6. #6
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    i dont understand

  7. #7
    lk9865 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    Can you use the java.util.Random class?
    Edit: random numbers without using java funtions
    aha - no java. Check the next method in the Random class for some clues.
    carlos,
    just like hardwired said there is another java class called Random you can upload before your programs you try something like this
    Java Code:
    java.util.Random;
    public class RandomProject{
    public staid void main(strings[] args){
    Random generator = new Random(); //give a object name for the class
    for(int i=0; i<=100; i++){
    int randomNumbers = generator.nextInt(20);//will generate numbers from1-20
    System.out.println(randomNumbers);
    }
    }
    }
    there you go you have now generated 100 random numbers between 1 and 20 hope this helps

  8. #8
    carlos123 is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    we cannot use any java functions

  9. #9
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    we cannot use any java functions
    Then try searching "generating random numbers" on Google and try to implement your own random number generator.

Similar Threads

  1. Computing prime numbers in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:39 PM
  2. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  3. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM
  4. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •