(**
 * Copyright (c) 2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the "hack" directory of this source tree.
 *
 *)

let () = Random.self_init ()

let base64_alphabet =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

let alphanumeric_alphabet =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

let () =
  assert (String.length base64_alphabet = 64)

let short_string_with_alphabet alphabet =
  let r = ref ((Random.bits () lsl 30) lor Random.bits ()) in
  let cs = ref [] in
  while !r > 0 do
    let c = alphabet.[!r mod (String.length alphabet)] in
    cs := String.make 1 c :: !cs;
    r := !r lsr 6
  done;
  String.concat "" !cs

let short_string () =
  short_string_with_alphabet base64_alphabet

(**
 * Copyright (c) 2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the "hack" directory of this source tree.
 *
 *)

let initialized = ref false

let base64_alphabet =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

let alphanumeric_alphabet =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

let () =
  assert (String.length base64_alphabet = 64)

let short_string_with_alphabet alphabet =
  (* If we haven't seeded random then do it now *)
  if not !initialized then begin
    initialized := true;
    Random.self_init ()
  end;
  let r = ref ((Random.bits () lsl 30) lor Random.bits ()) in
  let cs = ref [] in
  while !r > 0 do
    let c = alphabet.[!r mod (String.length alphabet)] in
    cs := String.make 1 c :: !cs;
    r := !r lsr 6
  done;
  String.concat "" !cs

let short_string () =
  short_string_with_alphabet base64_alphabet

Fix random generation of names
[hack] Fix random generation of names

Summary:
Test `hphp/hack/test/integration:run_watchman_event_watcher_client_test` was failing, seemingly because generation of temporary directory names wasn't getting seeeded
correctly by `Random_id.short_name ()`.

Haven't completely worked out why this failed, but this seems to fix it, and is clearer, as we don't rely on top-level side-effecting code.

Reviewed By: dabek

Differential Revision: D9980411

fbshipit-source-id: 4712b28dfdcb7ff134cf54d49a804661a56c00a6
