Solved

Question on Random.

Posted on 1998-09-05
1
739 Views
Last Modified: 2008-03-10
how can I generate random numbers say range between 1-255?
and what's the function used in JAVA to randomise the seed so that the same number won't be generated so often.

Thanks for any help offered!
0
Comment
Question by:justinng
1 Comment
 
LVL 2

Accepted Solution

by:
vendrig earned 30 total points
ID: 1223619
Here's the code to do it (I hope the tabs will survive;). I made an application so that you can test it easily. E.g., you should call it with <COMMAND>java MyRandom 1 255</COMMAND>. The function showRandomNumbers retrieves a random number between 0 and 1 and then maps it to the given range. It prints a list of 100 such numbers as an example. The same function can be used in an applet, and you can do with it what you want (like putting the numbers in an array).

import java.util.Random;
import java.lang.Math;

class MyRandom
{
      public static void main (String args[])
      {
            if (args.length!=2)
                  System.out.println("Usage: java MyRandom [minimum: Integer] [maximum: Integer]");
            else
                  showRandomNumbers(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
      }

      private static void showRandomNumbers(int lo, int hi)
      {
            // note: limits lo and hi are included in range
            Random r = new Random();      // default: seed based on current time
      // or:      Random r = new Random(123);      // fixed seed
            for (int i=0; i<100; i++)
            {
                  int randomNr=lo+(int) Math.round((hi-lo)*r.nextDouble());
                  System.out.print(randomNr+" ");
            }
      }
} // end class MyRandom

If you have JGL at your disposal, consider using COM.objectspace.jgl.Randomizer. This would reduce the amount of code, because you can simply use getInt(lo,hi) on a Randomizer object.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question