?
Solved

Question on Random.

Posted on 1998-09-05
1
Medium Priority
?
765 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
vendrig earned 90 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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