I'm using the code below to get a random number of items from an array, but I'd like to just get 3-5 random numbers from the provide range to display. Right now, it either shows 1, all, or any amount in between. I'm also good with completely changing up my strategy, so any and all advice is welcome. Much appreciated.

<script type="text/javascript" src="https://fiftyallstars.com/Code/jquery-2.2.4.js"></script>

<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>

<script type="text/javascript">
$('div').filter(function(){
    return (Math.round(Math.random()) == 1);
}).css({display: 'none'});
</script>

Recommended Answers

All 3 Replies

Where will those div values come from? Will it be known fixed (constant) values or dynamic?

define custom function that accepts list and returns element

get_random = function (list)
{
     resturn list [Math.floor((Math.random()*list.length))];
}

get_random([2,3,5])

Where will those div values come from? Will it be known fixed (constant) values or dynamic?

I don't think it really matters how or why we ended up with a series of <div>s that each contain a number. All that matters is they're in the DOM as so when it is time to select a handful of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.