Solved

Question on Random.

Posted on 1998-09-05
1
758 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 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 Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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…

717 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