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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:54:59+00:00 2026-05-28T03:54:59+00:00

So, I’ve got my code ready to generate and print a random number between

  • 0

So, I’ve got my code ready to generate and print a random number between “-20 and 20”.

What I’d like to do, and this is where I’m stuck, is to assign 4 different images to a number range between my total range of “-20 to 20”.

For instance:

  • image1.gif would be assigned to any random # between “-20 and -10”
  • image2.gif assigned to “-9 and 0”
  • image3.gif to “1 and 10”
  • image4.gif to “11 and 20”

Any suggestions much welcomed!

EDIT:
So based on Jon’s answer, i’ve changed the last two lines to a document.write and am trying to get my actual images shown above the text output. I’m however aware of a faulty line in the IMG.SRC. What would this be needed to change to if my images were lying in my root dir? Thanks in advance.

<html>
<title>RANDOM IMAGE TO NUMBER RANGE</title>
<body>
<center>
<script language="javascript" type="text/javascript">
var ranges = [
    { from: 11, image: "image4.jpg" },
    { from: 1, image: "image3.jpg" },
    { from: -9, image: "image2.jpg" },
    { from: -20, image: "image1.jpg" }       
];

var random_num = (Math.round((Math.random()*41)+-20));
var image = "default";

for(var i = 0; i < ranges.length; ++i) {
    if(random_num >= ranges[i].from) {
        image = ranges[i].image;
        break;
    }
}
document.write("Number: " + random_num + " parts, " + " Imagetitle: = " + image);
document.write('<img src="'+ranges[image]+'">');

</script>
<br></br>
<div>
CLICK <a href="javascript:history.go(0);">REFRESH or F5</a> FOR A RANDOM IMAGE on NUMBER-RANGE.
</div>
</center>
</body>
</html> 
  • 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-28T03:55:00+00:00Added an answer on May 28, 2026 at 3:55 am

    Put the ranges inside an array and iterate over it to find which range you are in:

    // IMPORTANT: array is sorted in descending "from" order!
    var ranges = [
        { from: 11, image: "image4" },
        { from: 1, image: "image3" },
        { from: -9, image: "image2" },
        { from: -20, image: "image1" }       
    ];
    
    var random_num = (Math.round((Math.random()*41)+-20));
    var image = "default";
    
    for(var i = 0; i < ranges.length; ++i) {
        if(random_num >= ranges[i].from) {
            image = ranges[i].image;
            break;
        }
    }
    
    alert("random_num = " + random_num + " / image = " + image);
    

    This will work correctly for intervals that may or may not be equal, and can easily be adapted to slightly more complex interval definitions.

    See it in action.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I would like to count the length of a string with PHP. The string
i got an object with contents of html markup in it, for example: string

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.