Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp2758793img; Sun, 24 Mar 2019 18:17:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqzYjz+BitZKTuTFV/NPoPRwAAq8E52qMCnRr6BuVpWoGZaBqBs4Ys1pAR2OnlYR0Jw+Dvxt X-Received: by 2002:a65:5003:: with SMTP id f3mr8089260pgo.29.1553476625568; Sun, 24 Mar 2019 18:17:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553476625; cv=none; d=google.com; s=arc-20160816; b=UepJQf08tazc4Ndf7iByuQ7ggd8eB8f0OiwRJ8yQ1gmiWVli1GxF8lpmPcXO7m5ftY mZo73cu1HRtmODuuAONTemcuxiEGteglARa5q8dtvXpil4Hkh61dulZsI45vFeHq7hw8 M6WHcfv4st5DmMWUMp88NvT26lIQfQ/ZQRmb3leeTMVridoS6skXxA2yWlqnr6K/nmGa epTGdENfcplct8n9JyGR9fc1jEHbEIb5iNIpZCywrQKoqT2efpj7QziUlcelb+wEPcUX SaMmGyQF570NeRKl0dujzWLCIvxoVqlpHlkYIqbFmf+HnCuvuHjh0mpbweG07rZH7DMP 4h3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:cc:subject:to :message-id:from:date; bh=+ScKS1S0N1f+J2wR+oEHdB+1cG9xpxgSPEDoiAwSW8U=; b=qbr4KnCuNPLpJ0juIFVRVcOsYZdtD+Ze25pXpUh6hAQuShbGztwECVKSLazDLbiZOq YotmKG8arCXSw74sHQKubH95MDXJeIRQeGyhyfWLKrAQx4FeBMDws4Dua118Kx0D09W/ e/+VuZokS/VVQWuZTHgs/KVMw2FiV375SD7LlG0vp8HNUGLx5KoKMmirAU8xcsC6v1IP GJAMGmpRwnhyrdbvYUopPqRp3OUZkcpRCJAUo49zFBBGnV4NjS7P90k8a4jLjhyAJqfG Y5CL1YNR2PFxnNtpiClQALMmWeySPNzAeaYnBUifPGJw1TXbKiFtLDPvuqMa+o9mDK1G onhQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g96si13586971plb.168.2019.03.24.18.16.11; Sun, 24 Mar 2019 18:17:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729150AbfCYBOf (ORCPT + 99 others); Sun, 24 Mar 2019 21:14:35 -0400 Received: from mx.sdf.org ([205.166.94.20]:64035 "EHLO mx.sdf.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729113AbfCYBOe (ORCPT ); Sun, 24 Mar 2019 21:14:34 -0400 Received: from sdf.org (IDENT:lkml@sdf.lonestar.org [205.166.94.16]) by mx.sdf.org (8.15.2/8.14.5) with ESMTPS id x2P1EU8d018718 (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256 bits) verified NO); Mon, 25 Mar 2019 01:14:30 GMT Received: (from lkml@localhost) by sdf.org (8.15.2/8.12.8/Submit) id x2P1EUQG004384; Mon, 25 Mar 2019 01:14:30 GMT Date: Mon, 25 Mar 2019 01:14:30 GMT From: George Spelvin Message-Id: <201903250114.x2P1EUQG004384@sdf.org> To: Jason@zx2c4.com, lkml@sdf.org Subject: Re: [RFC PATCH] random: add get_random_max() function Cc: linux-kernel@vger.kernel.org, tytso@mit.edu In-Reply-To: References: <201903241244.x2OCiL8P011277@sdf.org>, Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org P.S. The cited paper calls your algorithm the "OpenBSD algorithm" and has a bunch of benchmarks comparing it to others in Fisher-Yates shuffles of sizes 1e3..1e9. Including all overhead (base PRNG, shuffle), it's 3x slower for 32-bit operations and 8x slower for 64-bit up to arrays of size 1e6, after which cache misses slow all algorithms, reducing the ratio. If you want a faster division-based agorithm, the "Java algorithm" does 1+retries divides: unsigned long java(unsigned long s) { unsigned long x, r; do { x = random_integer(); r = x % s; } while (x - r > -s); return r; }