0

The randomizer for width and height works, but it only gets first random photo from collection and generates 10 same images of various dimensions.

export const photos = Array.from({length:10},()=> 
    (
        {
            'src': 'https://source.unsplash.com/random',
            'width': Math.floor(Math.random() * 4) + 2,
            'height': Math.floor(Math.random() * 4) + 2
        }
    )
) 

What I would want is that it fills the array with 10 different pictures using that same link.

Bratelion
  • 11
  • 3
  • 3
    You aren't changing the `src` in any way. – pilchard Jan 12 '21 at 11:15
  • But opening that `src` every time changes the picture. Try opening that link several times – Bratelion Jan 12 '21 at 11:18
  • 2
    Doesn't seem like a JavaScript problem at all. If you include multiple images with this URL [the service seems to produce the same picture](https://jsbin.com/peberuvega/edit?html,output). – VLAZ Jan 12 '21 at 11:18
  • 1
    [Seems like you can append unique query strings to get different images](https://jsbin.com/giherovala/1/edit?html,output) – VLAZ Jan 12 '21 at 11:20
  • Your browser probably caches the first image it receives. like VLAZ said, you can add a query string to act as a [cache buster](https://stackoverflow.com/questions/9692665/cache-busting-via-params), so your browser will have a unique URL to cache separately. – cbr Jan 12 '21 at 11:24
  • @VLAZ That solves it. Thank you so much! – Bratelion Jan 12 '21 at 11:31

2 Answers2

1

Try this

const photos = Array.from({length:10},()=> ({'src': `https://picsum.photos/id/${Math.ceil(Math.random()*999)}/${Math.floor(Math.random() * 4) + 2}/${Math.floor(Math.random() * 4) + 2}`}))

console.log(photos)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

@VLAZ solved it. Appending some query string gets me random images.

export const photos = Array.from({length:10},()=> 
    (
        {
            'src': 'https://source.unsplash.com/random?' + Math.floor(Math.random() * 100) + 1,
            'width': Math.floor(Math.random() * 4) + 2,
            'height': Math.floor(Math.random() * 4) + 2
        }
    )
)
Bratelion
  • 11
  • 3