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

40
LINES

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

JavaX fragment (include)

static Random random_random = new Random;

static int random(int n) {
  return random(random_random, n);
}

static int random(Random r, int n) {
  return n <= 0 ? 0 : r.nextInt(n);
}

static double random(double max) {
  return random()*max;
}

static double random() {
  return random_random.nextInt(100001)/100000.0;
}

static double random(double min, double max) {
  return min+random()*(max-min);
}

// min <= value < max
static int random(int min, int max) {
  return min+random(max-min);
}

static int random(Random r, int min, int max) {
  return min+random(r, max-min);
}

static <A> A random(L<A> l) {
  ret oneOf(l);
}

static <A> A random(Collection<A> c) {
  if (c instanceof L) ret random((L<A>) c);
  int i = random(l(c));
  ret collectionGet(c, i);
}

download  show line numbers  debug dex   

Travelled to 12 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, podlckwnjdmb, tslmcundralx, tvejysmllsmz, 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/7
Text MD5: 702dfb4fe1bac497b0cc337b911bddbb
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-12 13:05:16
Source code size: 805 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 553 / 1132
Version history: 6 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1002033 & http://1002033.tinybrain.de