Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22158

Re: random number

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.144
X-Spam-Level *
X-Spam-Evidence '*H*': 0.71; '*S*': 0.00; 'subject:number': 0.09; 'random': 0.13; 'encryption': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helps!': 0.16; 'subject:random': 0.16; 'argument': 0.18; 'mon,': 0.18; 'def': 0.20; 'received:209.85.210.174': 0.20; 'received:mail- iy0-f174.google.com': 0.20; 'wrote:': 0.21; 'string,': 0.22; 'header:In-Reply-To:1': 0.22; 'quite': 0.26; 'message- id:@mail.gmail.com': 0.27; 'pm,': 0.28; 'received:209.85': 0.32; 'received:209.85.210': 0.32; 'chris': 0.32; 'received:google.com': 0.32; 'are:': 0.32; 'easiest': 0.33; 'received:209': 0.35; 'two': 0.35; 'there': 0.35; '\xa0\xa0\xa0': 0.36; 'but': 0.36; 'base': 0.39; 'to:addr:python-list': 0.39; 'how': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; 'mar': 0.61; 'guarantee': 0.62; 'skip:n 10': 0.62; 'ways': 0.63; 'avoid': 0.63; 'easy': 0.65; 'hope': 0.65; 'giving': 0.66; '26,': 0.66; '2012': 0.69; 'six': 0.81; 'different.': 0.84; 'alphanumeric': 0.93; 'technique': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=bSIm9M1/41YeUMNd1zGWwDYNgsr8uaF2HfPtfiI5se8=; b=pMb6aX76w7LRWA2Pxg4qBk9XCXNU7UoQKoavNYxk3SYiI868y+XgD9Sw1CW9dDXrte te1lOVfAwDaucMc9Us6pcGCXV1iKB/iQEw9P24slFDFe16ezR/d9jbmxW4siIpPGrum9 jMuQwd5gy72E/ojNdENSen+MeR9IzGavYGq3Zf68FrUIK0NLjrqbDlkUnXv5stRGBr4j f+GMrsVdm8dE3pbHaGJwuJ7D5b5WKMkmSTTFmu00u8csL493J/EGYEjIRvOA3NXluAH9 yTLpb2smkMjXoXB+YPbSJpTllb+UcSa9u/7kXhMyiIFF/z9LiCF0I927oYg0B3EwSGgX rGrg==
MIME-Version 1.0
In-Reply-To <CABgq=FyhR+Ldujj3YKRBpVXRVoeoayuXZviUUcNx-gXnFiHLSw@mail.gmail.com>
References <CABgq=FyhR+Ldujj3YKRBpVXRVoeoayuXZviUUcNx-gXnFiHLSw@mail.gmail.com>
Date Mon, 26 Mar 2012 17:25:06 +1100
Subject Re: random number
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.979.1332743109.3037.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332743109 news.xs4all.nl 6989 [2001:888:2000:d::a6]:58487
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22158

Show key headers only | View raw


On Mon, Mar 26, 2012 at 5:08 PM, Nikhil Verma <varma.nikhil22@gmail.com> wrote:
> Hi All
>
> How can we generate a 6 digit random number from a given number ?
>
> eg:-
>
> def number_generator(id):
>     random.randint(id,999999)
>
> When i am using this it is sometimes giving me five digit and sometimes 6 .
> I want to avoid encryption . Can i have alphanumeric 6 digit random number
> from this .

The easiest two ways to guarantee six digits are:
1) Pad the number with leading zeroes:
def number_generator():
  return "%06d"%random.randint(0,999999)
2) Set a minimum and a maximum:
def number_generator():
  return random.randint(100000,999999)

I don't know what your id there is, but the first argument to randint
is the minimum value to return.

Alphanumeric is quite different. To generate a six-character random
alphanumeric string, one easy technique is to use base 36 conversion
on a random integer.

Hope that helps!

Chris Angelico

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: random number Chris Angelico <rosuav@gmail.com> - 2012-03-26 17:25 +1100

csiph-web