Skip to content

Instantly share code, notes, and snippets.

@xhiroga
Created June 20, 2019 23:52
Show Gist options
  • Save xhiroga/889dcdd922c6b9a44a387084eb4b2d83 to your computer and use it in GitHub Desktop.
Save xhiroga/889dcdd922c6b9a44a387084eb4b2d83 to your computer and use it in GitHub Desktop.
Pick an element from an array every day, with 3 conditions. 1. while a loop, the element should not be same. 2. for every loop, order should be random. 3. no external memory.
Pick an element from an array every day, with 3 conditions. 1. while a loop, the element should not be same. 2. for every loop, order should be random. 3. no external memory.
ある配列から、毎日要素を取り出したい。ただし、配列が一周するまでは要素が重複してはいけず、また、毎周の順番が固定でもいけない。
an array.
ある配列がある。
```
["apple", "banana", "carrot"]
```
Show like this.
これを、次のように表示する。
1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9
apple, banana, carrot, banana, apple, carrot, apple, carrot, banana, ...
Without external memory. It means, there are no way to remember what is shown in the past. What program only knows is just array and the date of today.
外部記憶なしでやること。つまり、昨日何を表示したとかはプログラムには与えられない。単に日付と配列のみで実装すること。
Any ideas?
どんなやり方が考えられますか?
@xhiroga
Copy link
Author

xhiroga commented Jun 21, 2019

Senior engineer in my team tells me this article.
https://en.wikipedia.org/wiki/Linear_congruential_generator

@kentrino
Copy link

p, q: large prime number
mMax = length!
m = (unixday x p % mMax)
nMax = length
m = unixday x q % nMax
pe = getPermutationsFrom(m)
return pe[n]

@kentrino
Copy link

遅かった、、

@xhiroga
Copy link
Author

xhiroga commented Jun 21, 2019

getPermutationsFrom こんなのあるんですね。勉強になります:bow:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment