Skip to content

Commit

Permalink
make shuffle use scheme random with seed #267
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 14, 2024
1 parent 59f0683 commit e2352a5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![npm](https://img.shields.io/badge/npm-1.0.0%E2%80%93beta.17.3-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
![1.0.0 Complete](https://img.shields.io/github/milestones/progress-percent/jcubic/lips/1?label=1.0.0%20Complete)
[![Build and test](https://github.com/jcubic/lips/actions/workflows/build.yaml/badge.svg?branch=devel&event=push)](https://github.com/jcubic/lips/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&588ba056f11c7ab86ecec631717742c3)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&98580fbea70da2365f3cca5d712703c5)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Join Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jcubic/lips)
![NPM Download Count](https://img.shields.io/npm/dm/@jcubic/lips)
![JSDelivr Download count](https://img.shields.io/jsdelivr/npm/hm/@jcubic/lips)
Expand Down
4 changes: 2 additions & 2 deletions dist/lips.ems.min.js

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions dist/lips.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions dist/lips.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/lips.min.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -2369,14 +2369,14 @@ function flatten(array, mutable) {
// :: Fisher-Yates (aka Knuth) Shuffle
// :: ref: https://stackoverflow.com/a/2450976/387194
// ----------------------------------------------------------------------
function shuffle(array) {
function shuffle(array, random) {
let currentIndex = array.length, randomIndex;

// While there remain elements to shuffle.
while (currentIndex > 0) {

// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
randomIndex = Math.floor(random() * currentIndex);
currentIndex--;

// And swap it with the current element.
Expand Down Expand Up @@ -7608,14 +7608,15 @@ var global_env = new Environment({
// ------------------------------------------------------------------
shuffle: doc(function(arg) {
typecheck('shuffle', arg, ['pair', 'nil', 'array']);
const random = global_env.get('random')
if (arg === nil) {
return nil;
}
if (Array.isArray(arg)) {
return shuffle(arg.slice());
return shuffle(arg.slice(), random);
}
let arr = global_env.get('list->array')(arg);
arr = shuffle(arr);
arr = shuffle(arr, random);

return global_env.get('array->list')(arr);
}, `(shuffle obj)
Expand Down
6 changes: 5 additions & 1 deletion tests/core.scm
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,13 @@

(test "core: shuffle"
(lambda (t)
;; test shuffle with fixed seed
(random 1000)
(t.is (shuffle '(1 2 3 4)) '(2 4 3 1))
(t.is (list? (shuffle '(1 2 3))) #t)
(t.is (shuffle nil) nil)
(t.is (vector? (shuffle #(1 2 3))) #t)))
(random 1000)
(t.is (shuffle #(1 2 3 4)) #(2 4 3 1))))

;; TODO
;; begin*
Expand Down

0 comments on commit e2352a5

Please sign in to comment.