T
T
toster1112021-07-19 15:22:01
JavaScript
toster111, 2021-07-19 15:22:01

How to shuffle an array in a pseudo-random sequence?

Good afternoon!
Imagine that we have two entities, for example, articles (article A and article B)
Each article has an array of hashtags (if the articles belong to the same topic, then the arrays of hashtags are identical, if they are different, they are different), in our case article A and article B belong to the same topic.

There is a need to shuffle each array in a pseudo-random manner, so that the article A page always displays the hashtags in one order, and the article B page displays the hashtags in a different order. That is, the random shuffle algorithm should always return the same shuffle for the same article, but may return different shuffles for different articles.
Accordingly, for articles from different topics, the arrays will be different, but the principle of the same mixing for the same article should be preserved

. Obviously, sorting the array by randomly generated numbers will not work here.
There was an idea to generate numbers based on the unique article identifier (id of the form 2613221) and sort on them, but it is not entirely clear how to do this, given that the length of the hashtag array is unknown
. What other options can there be?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2021-07-19
@gbg

The random number generator already works based on some starting value (seed). So if you feed him the article ID, he will always stamp the same, but random set of numbers.

L
Leonid Nikolaev, 2021-07-19
@nikonor

Yes, divide 1000000 by this id and run through the numbers after the decimal point. It is also important to get just a conditionally infinite, but the same sequence of numbers

W
Wataru, 2021-07-19
@wataru

There are many different solutions. You can take any shuffling algorithm and initialize the random number generator with the article number.
Or you can sort the hashes obtained from the tag id and the article id. For example, you can count tag_id*article_id % n.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question