Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:32:35+00:00 2026-06-15T23:32:35+00:00

import java.util.Random; public class Fighter { int str; int dex; int hth; Random gen

  • 0
import java.util.Random;

public class Fighter {
int str;
int dex;
int hth;
Random gen = new Random(1535636);   

public Fighter() {
     str = gen.nextInt(9);
     dex = gen.nextInt(9);
     hth = gen.nextInt(14);
}


public int getHth(){

   return hth;

   }

public int getStr(){
   return str;
   }

public int getDex(){
   return dex;
       }
 }


import java.util.Random;
public class Arena {


public static void main(String[] args) {
    Random gen = new Random();
      Fighter Guy1 = new Fighter();
      int x =1;
              while (x < 200000000){
                x++;  
              }
      Fighter Guy2 = new Fighter();

    int hth1 = Guy1.getHth();
    int hth2 = Guy2.getHth();

    System.out.println("Fight Start");
    System.out.println("---------------");
    //System.out.println(gen.nextInt(10));
    //System.out.println(gen.nextInt(17));
    System.out.println(Guy1.getStr());

    //Fighting



}



}

Whenever i run this i get the same results no matter what. I’d like it to make 2 random fighters each time. Right now theres a few lines that were just to confirm that it doesn’t make random numbers.

Does anyone know how to use random numbers in a constructor properly? or am i doing this completely wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:32:37+00:00Added an answer on June 15, 2026 at 11:32 pm

    You’re providing a constant seed value to the random number generator:

    Random gen = new Random(1535636);  
    

    Don’t do that. It will always provide the same values. Just call the default constructor:

    Random gen = new Random();
    

    That

    Creates a new random number generator. Its seed is initialized to a value based on the current time:

    http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#Random()


    Why?

    As with most “standard library” random number generators, Random is a “Pseudorandom number generator”. That means it is not actually generating random numbers! Instead, it is calculating them in a very defined fashion – they just look like random numbers, and they tend to have a decent distribution.

    PRNGs are initialized with a seed value which sets their internal state. If you provide the same seed value every time, the PRNG is going to be in the same state every time you run it, and thus, provide the same sequence of numbers!

    The thing that makes them seem random all the time, is that usually1 they are “seeded” by default with a time-based value. In most libraries this is a time-dervied value with very good precision. So most of the time, if you see someone seeding their PRNG, it is probably incorrect, or at least very unnecessary.

    1 – Note: This is not the case with rand() from libc: “If no seed value is provided, the rand() function is automatically seeded with a value of 1.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.Random; import java.util.Scanner; public class Lottery { private int[] lotteryNumbers = new int[5];
import java.util.Random; class VehicleConstructor{ private int startSpeed; public VehicleConstructor() { setSpeed(); System.out.println(VehicleConstructor); } private
import java.util.Random; public class dice { private int times; private int roll; private int
//Vehicle.java import java.util.Random; class VehicleConstructor{ private int speed; private int timeDriven; private int run;
I would like to know why this code: import java.io.*; import java.util.Random; public class
import java.util.Random; import java.util.Scanner; public class Game { public static void main(String[] args) {
Eg. - take the class import java.awt.Color; import java.util.Random; import javax.swing.JLabel; public class flashThread
import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Frame; import java.awt.Graphics; import java.awt.Color; import java.util.Random; public class
import java.util.Scanner; import java.util.Random; public class DrawTriangle { public static void main(String[] args) {
import java.util.ArrayList; import java.util.Random; public class Generator{ //9352141NTBG1223 public static void main(String[] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.