Solved

Question on Random.

Posted on 1998-09-05
1
728 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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Complex Is This Java Course ? 9 62
Starting to learn JAVA, 7 47
create a gui in perl 3 42
solarwind tftp server 2 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now