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 7576641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:58:18+00:00 2026-05-30T16:58:18+00:00

Is there a way to pick a random object from an array of objects?

  • 0

Is there a way to pick a random object from an array of objects?

I have tried a few ways that I could think of but none work.
I want to use a function in the random class (all classes will have the same function but different returns).

I should have mentioned i am trying to return an image, and each time i try to return a random image it doesnt return anyting (“null”)

Here is some of my code I am having the problem with:

    Car car;
    Ford ford;
    Mazda mazda
    Fiat fiat
    Rover rover   

    Car carlist[] = {ford,fiat,mazda,rover}

public void paint(){
//this displays an image every 128 pixles 
        for (int i = 0;i<Width;i+=128){

            for(int j=128; j<Height;j+=128){
// this draws the image (the image is declaired in each car's class as getImage)
                g.drawImage((car.carList[rand.nextInt(5)]).getImage(), i, j , this);
                    }
                }

The code works if i put an object in (instead of carcarList[rand.nextInt(5)])).
Each of the cartypes extend Cars.

  • 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-05-30T16:58:19+00:00Added an answer on May 30, 2026 at 4:58 pm

    Assuming yourList is the array of your objects, use this single line:

    Object randomObj = yourList[(int)Math.random() * list.length];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in factory_girl to specify a random instance that an association
After reading this answer: best way to pick a random subset from a collection?
Could someone tell me how can I pick several different random numbers from an
I'm trying to pick a random element from an array -- is this possible
I'm thinking if there is a way to use javascript to pick up ANY
is there way how to get name ov event from Lambda expression like with
Is there a way to find the name of the program that is running
I'm trying to figure out a way to create random numbers that feel random
In my app, I want to pick a random image. Each image that I
In my grails application I have an sql query that selects column data from

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.