🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.java
Article #26517

Re: Pregunta sobre el random

#26517
From: Rehm
Date: Wed, 23 Dec 1998 00:00
109 lines
3531 bytes
On Mon, 21 Dec 1998 23:11:42 +0100, Rafael Osuna
<ROSUNA@santandersupernet.com> wrote:

>Hola!
>
>    Mi pregunta al grupo es la siguiente, �Que puedo utilizar para hacer
>un random, igual que en c, en un javascript de una pagina web? hay
>alguna funcion especifica, la funcion math.random() solo devuelve 0 o 1,
>no me sirve, yo quiero un random de verdad :-)
>
>Gracias y un saludo a todos.


Ahi te va:

                     J  D  C    T  E  C  H    T  I  P  S


                     TIPS, TECHNIQUES, AND SAMPLE CODE
                          * True/False Random Values with NextBoolean
                          * Cross Compilation Using -Target
                          * Unicode Character Blocks




- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-


T I P S ,  T E C H N I Q U E S ,   A N D   S A M P L E   C O D E


TRUE/FALSE RANDOM VALUES WITH NEXTBOOLEAN


nextBoolean is a new method, which is part of the java.util.Random
class.
It returns a random false/true value, and is useful if you have an
application that effectively needs to flip a coin.


Random numbers are especially useful when generating test data.


This program prints out 10 values of "heads" or "tails":


        import java.util.Random;


        public class flip {
                public static void main(String args[])
                {
                        Random rn = new Random();


                        for (int i = 1; i <= 10; i++)
                                System.out.println(rn.nextBoolean() ?
                                    "heads" : "tails");
                }
        }


If you've studied random numbers at all, especially the method used by
Java
(the linear congruential method), you might recall that one problem
with
generated random numbers is that the low-order bits tend to be less
random
than the high-order ones.  This problem is worked around by internally
generating a sequence of 48-bit random values, and then using at most
32 of
the high-order bits of any given value.  nextBoolean uses a single
high
bit, while nextInt uses 32 bits of the 48, ignoring the lowest 16
bits.
See Knuth's "The Art of Computer Programming" section 3.2.1 for a
discussion of random number generators.


Another new feature in Random is the ability to generate random
integers
in a specified range, using nextInt. nextInt(N) returns a random
integer in the range:


        0 <= number < N


So this program will generate a sequence of 10 numbers, each in the
range 0-9 inclusive:


        import java.util.Random;


        public class rand {
                public static void main(String args[])
                {
                        Random rn = new Random();


                        for (int i = 1; i <= 10; i++)
                                System.out.println(rn.nextInt(10));
                }
        }
-------
uov00973ARROBAcorreo.uniovi.es
Technical Details
Message-ID: <368c4855.60680874@noticias.ibernet.es>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <367EC79E.B8FA293D@santandersupernet.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 1116f3,ccf22a38740b2927
X-Google-Attributes: gid1116f3,public
From: uov00973ARROBA@correo.uniovi.es (Rehm)
Subject: Re: Pregunta sobre el random
Date: 1998/12/23
Message-ID: <368c4855.60680874@noticias.ibernet.es>
X-Deja-AN: 425033056
Content-Transfer-Encoding: 8bit
References: <367EC79E.B8FA293D@santandersupernet.com>
Content-Type: text/plain; charset=ISO-8859-1
Organization: Telefonica Transmision de Datos
Mime-Version: 1.0
Newsgroups: es.comp.lenguajes.java