Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

78
LINES

< > BotCompany Repo | #1002033 // random - return random number between 0 and n-1, and other version

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (9634L) is out of date.

1  
static int random(int n, Random r default defaultRandomGenerator()) {
2  
  ret random(r, n);
3  
}
4  
5  
static long random(long n, Random r default defaultRandomGenerator()) {
6  
  ret random(r, n);
7  
}
8  
9  
static int random(Random r, int n) {
10  
  return n <= 0 ? 0 : getRandomizer(r).nextInt(n);
11  
}
12  
13  
static long random(Random r, long n) {
14  
  return n <= 0 ? 0 : getRandomizer(r).nextLong(n);
15  
}
16  
17  
static double random(double max) {
18  
  return random()*max;
19  
}
20  
21  
static double random() {
22  
  return defaultRandomGenerator().nextInt(100001)/100000.0;
23  
}
24  
25  
static double random(double min, double max) {
26  
  return min+random()*(max-min);
27  
}
28  
29  
// min <= value < max
30  
static int random(int min, int max) {
31  
  return min+random(max-min);
32  
}
33  
34  
// min <= value < max
35  
static long random(long min, long max) {
36  
  return min+random(max-min);
37  
}
38  
39  
static int random(int min, int max, Random r) {
40  
  ret random(r, min, max);
41  
}
42  
43  
static int random(Random r, int min, int max) {
44  
  return min+random(r, max-min);
45  
}
46  
47  
static <A> A random(L<A> l) {
48  
  ret oneOf(l);
49  
}
50  
51  
static <A> A random(Collection<A> c) {
52  
  if (c instanceof L) ret random((L<A>) c);
53  
  int i = random(l(c));
54  
  ret collectionGet(c, i);
55  
}
56  
57  
ifclass IntRange
58  
static int random(IntRange r) {
59  
  ret random(r.start, r.end);
60  
}
61  
endif
62  
63  
ifclass DoubleRange
64  
static double random(DoubleRange r) {
65  
  ret random(r.start, r.end);
66  
}
67  
endif
68  
69  
ifclass TimestampRange
70  
static Timestamp random(TimestampRange r) {
71  
  ret r == null ?: new Timestamp(random(r.startTime().unixDate(),
72  
    r.endTime().unixDate()));
73  
}
74  
endif
75  
76  
static <A, B> Pair<A, B> random(Map<A, B> map) {
77  
  ret entryToPair(random(entries(map)));
78  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wnsclhtenguj, xrpafgyirdlv

No comments. add comment

Snippet ID: #1002033
Snippet name: random - return random number between 0 and n-1, and other version
Eternal ID of this version: #1002033/21
Text MD5: e9c9d217c6384a9e1a0b229f56fd2a60
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-15 15:04:54
Source code size: 1667 bytes / 78 lines
Pitched / IR pitched: No / No
Views / Downloads: 951 / 1849
Version history: 20 change(s)
Referenced in: [show references]