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

52
LINES

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

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

Libraryless. Click here for Pure Java version (5094L/28K).

1  
static int random(int n, Random r default defaultRandomGenerator()) {
2  
  ret random(r, n);
3  
}
4  
5  
static int random(Random r, int n) {
6  
  return n <= 0 ? 0 : getRandomizer(r).nextInt(n);
7  
}
8  
9  
static double random(double max) {
10  
  return random()*max;
11  
}
12  
13  
static double random() {
14  
  return defaultRandomGenerator().nextInt(100001)/100000.0;
15  
}
16  
17  
static double random(double min, double max) {
18  
  return min+random()*(max-min);
19  
}
20  
21  
// min <= value < max
22  
static int random(int min, int max) {
23  
  return min+random(max-min);
24  
}
25  
26  
static int random(int min, int max, Random r) {
27  
  ret random(r, min, max);
28  
}
29  
30  
static int random(Random r, int min, int max) {
31  
  return min+random(r, max-min);
32  
}
33  
34  
static <A> A random(L<A> l) {
35  
  ret oneOf(l);
36  
}
37  
38  
static <A> A random(Collection<A> c) {
39  
  if (c instanceof L) ret random((L<A>) c);
40  
  int i = random(l(c));
41  
  ret collectionGet(c, i);
42  
}
43  
44  
ifclass IntRange
45  
static int random(IntRange r) {
46  
  ret random(r.start, r.end);
47  
}
48  
endif
49  
50  
static <A, B> Pair<A, B> random(Map<A, B> map) {
51  
  ret entryToPair(random(entries(map)));
52  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, 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/16
Text MD5: 21fb6b9bec969408ec1c7fe2202cd22f
Transpilation MD5: a2dc269e9fd08894a85ed2d5545be21a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-27 14:08:02
Source code size: 1087 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 751 / 1534
Version history: 15 change(s)
Referenced in: [show references]