practicalli / practicalli/clojure

Generate random integer numbers

Open
#120 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Makefile
Stars
117
Forks
36
PR merge metrics
No merged PRs in 30d

Description

(repeatedly 10 #(rand-int 5))

or an infinite lazy list with
(repeatedly #(rant-int 5)

(let [ powerball-numbers       (repeatedly #(rand-int 100))
       my-powerball-numbers    (take 10 powerball-numbers) ;; Clearly I don't play powerball
       their-powerball-numbers (take 10 powerball-numbers)]
  {:mine my-powerball-numbers 
   :theirs their-powerball-numbers})

my-powerball-numbers and their-powerball-numbers are the same

I did not know it would not produce fresh values each time

lazy-seqs are immutable and cached, reading the nth item of a given lazy-seq always returns the same value

I'm playing with it now, I see they work / produce fresh values when produced from separate function calls, but not when created in the same let

if you call repeatedly again, you get a different lazy-seq, but if you reuse the same lazy-seq (the return of one call to repeatedly) you'll get the same values in the same order

so clearly what you need is (repeatedly (fn [] (repeatedly #(rand-int 5))))
or maybe not :D

a fun way to do your pass/fail above: #(rand-nth (into [] cat [(repeat 7 "Passed!") (repeat 3 "Failed!")])

better to construct that vector outside the function or hard code it actually

personally, as an optimist, I'd like to go with (repeat 10 "Passed!")

in that case you can just use (constantly "Passed!")

oh never mind, if you use repeat you don't need constantly

also useful if you have more transformations to add there, to eventually create a stream generating something else entirely

(let [ ;; This stream produces random 'pass' 'fail' scenarios
grade-simulator (->> (repeatedly #(rand-int 100)) ;; Stream of random numbers
(map (fn [grade]
(if (> grade 70) :pass :fail)) ;; Oop, now a stream of 'pass' / 'fail' symbols
;; This stream produces random messages reflecting that scenario you might print out, like 'Passed!'
grade-message (map (fn [pass-or-fail]
(if (= pass-or-fail :pass) "Passed!" "Failed!"))
grade-simulator)]
(take 34 grade-message)) ;; "Passed!" "Failed!" "Passed!" "Passed"...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository file or test; start by reviewing its Clojure repeatedly examples and locating the related documentation or lesson. Done would require a clearly scoped explanation or example of the intended random-number behavior, but the issue does not define the target change or validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.