random numbers

How can i choose randomly one of the numbers 1,2,3 ?
[59 byte] By [x_3fPStaa] at [2007-10-3 3:33:47]
# 1
One two thre and to the fo'Snoop Doggy Dogg and Dr. Dre is the the do'
RadcliffePikea at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 2
java.util.Random.nextInt(int n)
jverda at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 3

hi x_3fPSta,

Here is a small program that displays numbers randomly from 1 to 3 below:

// PickRandomNumbers.java

// PickRandomNumbers uses random to pick numbers from 1 to 3.

import java.util.Random;// program uses class Random

import java.util.Scanner; // program uses class Scanner

public class PickRandomNumbers

{

// main method begins program execution

public static void main( String args[] )

{

Random randomNumbers = new Random();

int startRandomNumbers = 1;// this is the starting position for where the

// random numbers start to generate

int maxRandomNumber = 3;// this is the max random number

Scanner input = new Scanner(System.in);

boolean wantContinue;// for the do while loop condition for checking choice

int choice; // 1 for yes or 0 for no choice

do // this do while loop randomly chooses a number from 1 to 3

{

int chooseRandomNumber = startRandomNumbers + randomNumbers.nextInt(maxRandomNumber);

System.out.printf("\n%s%d\n\n","The random number is ",chooseRandomNumber);

do // this do while loop test if the choice is either 1 or 0 or other number

{

System.out.printf("Do you want to continue? 1 for yes , 0 for no : ");

choice = input.nextInt();

if (choice == 1 || choice == 0)

wantContinue = false;

else // if choice is other than 1 or 0

wantContinue = true;

}

while (wantContinue != false);

}

while (choice != 0);

} // end main

} // end class PickRandomNumbers

egan128a at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 4

Hi x_3fPSta

I have another program that I have written that generates random numbers from1 to 20. There is a pressing enter key to continue.

Also the choices are y for yes and n for no. This program is similar to the other program for choicing random numbers between 1 and 3.

Here is the program code:

// PickRandomNumbers.java

// PickRandomNumbers uses random to pick numbers from 1 to 20.

import java.util.Random;// program uses class Random

import java.util.Scanner; // program uses class Scanner

public class PickRandomNumbers

{

// main method begins program execution

public static void main( String args[] )

{

Random randomNumbers = new Random();

int startRandomNumbers = 1;// this is the starting position for where the

// random numbers start to generate

int maxRandomNumber = 20;// this is the max random number

Scanner input1 = new Scanner(System.in);

Scanner input2 = new Scanner(System.in);

String theInput;

boolean wantContinue;// for the do while loop condition for checking choice

char choice;// y for yes or n for no choice

do // this do while loop randomly chooses a number from 1 to 20

{

int chooseRandomNumber = startRandomNumbers + randomNumbers.nextInt(maxRandomNumber);

System.out.printf("\n%s%d\n\n","The random number is ",chooseRandomNumber);

System.out.println("Press Enter to continue ...");

input1.useDelimiter("\n");

String garbage = input1.next();

System.out.println();

do // this do while loop test if the choice is either y or n or other character

{

System.out.printf("Do you want to continue? y for yes , n for no : ");

theInput = input2.next();

choice = theInput.charAt(0);

if (choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N')

wantContinue = false;

else // if choice is other than y or n

wantContinue = true;

}

while (wantContinue != false);

}

while (choice == 'y' || choice == 'Y');

} // end main

} // end class PickRandomNumbers

egan128a at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 5
Seeing as how it's friday why not get a discussion going - is there such thing as a random number ? Or should this post say pseudo-random number ?
Aknibbsa at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 6

> I have another program that I have written that

> generates random numbers from1 to 20. There is a

> pressing enter key to continue.

> Also the choices are y for yes and n for no. This

> program is similar to the other program for choicing

> random numbers between 1 and 3.

egan128: You shouldn't call the maxRandomNumber the "max". Try setting your "startRandomNumber" to 5 and your so-called "maxRandomNumber" to 20. Your range will not be "5 to 20".

MLRona at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 7

> Seeing as how it's friday why not get a discussion

> going - is there such thing as a random number ? Or

> should this post say pseudo-random number ?

Unless you've got a hardware-based random number generator (e.g. something that uses the radioactive decay of some element, or reads background microwave radiation from deep space), it will be pseudo-random. However, in most cases, we just say "random" for brevity and because it's understood--and not relevant--that it's not truly random.

jverda at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 8
Is it possible for a machine to never generate a particular number?When you flip a coin you dont ever HAVE to get a heads.So the way Java's random numbers work are you guarenteed to not have that happen?Just curious.
TuringPesta at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 9

> Is it possible for a machine to never generate a

> particular number?

One could create a PRNG that will never generate a particular number. But that doesn't make it random, because it is determined a priori that the probability of getting that number is always zero.

If you have a hardware-based RNG, then any sequence is possible. If you run it for an infinitely long time, every number must come up, but for any finite time, any distribution is possible.

> When you flip a coin you dont ever HAVE to get a

> heads.

> So the way Java's random numbers work are you

> guarenteed to

> not have that happen?

In Java's PRNGs, you will get every number they are capable of producing in one cycle length. That is, once you start from a given point, you will walk a predictable path through every combination that can occur.

jverda at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 10

Hi,

Since this is a newby corner of the forum, and I am a newby, and I actually didn't understand what all these people are talking about, and I know a simple answer ... :)

There is something that is called Math.random()

that generates a random "double" number from 0 to 0.99999....

So if you for example wish to have the numbers 1 to 3 then you need to multiply that random number like this Math.random()*3

.

Now you have a "double" random number from 0 to 2.9999...

To get the number 1 to 3, you now need to add 1 (Math.random()*3)+1

.

Now you have a random number from 1 to 3.999...

But you want an integer, and not a double. You fix it like this (int)((Math.random()*3)+1)

So you should write in your code:

int randomNumber = (int)((Math.random()*3)+1);

Works every time, guaranteed! Have fun programming! Greetz, Daffn.

Daffna at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 11
> and I know a simple answerA swing and a miss! ; )java.util.Random random = new java.util.Random();int number = random.nextInt(3);will give an int 0,1,2 http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html
TuringPesta at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 12

> > and I know a simple answer

>

> A swing and a miss! ; )

>

> java.util.Random random = new java.util.Random();

> int number = random.nextInt(3);

>

> will give an int 0,1,2

>

> http://java.sun.com/j2se/1.4.2/docs/api/java/util/Rand

> om.html

Why?

int randNum = (int)(Math.random() * 3 + 1);

That will give him a random number between 1 and 3.

CaptainMorgan08a at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 13

It definitely will but i dont believe its the best answer.

First, I dont think its as clear.

Second, math.random() calls new java.util.Random a la the docs

"When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random"

And id rather just call it myself ; )

Third, if you read the util.Random documentation they provide the code

by which they get the int and its not by the(x*number + 1) way and

for good reason. Check it out.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#nextInt(int)

TuringPesta at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 14
Ive actually already read the code before. I use the Random class myself, I just wouldnt have described it as a "swing and a miss."
CaptainMorgan08a at 2007-7-14 21:28:13 > top of Java-index,Java Essentials,New To Java...
# 15
Just good natured ribbing. No disrespect intended. Hence the ; ) Later.
TuringPesta at 2007-7-21 10:13:09 > top of Java-index,Java Essentials,New To Java...
# 16

> There is something that is called

> Math.random()

that generates a random

> "double" number from 0 to 0.99999....

Yes.

That is well known, and is not the best solution here.

> So if you for example wish to have the numbers 1 to 3

> then you need to multiply that random number like

> this Math.random()*3

.

> Now you have a "double" random number from 0 to

> 2.9999...

> To get the number 1 to 3, you now need to add 1

> (Math.random()*3)+1

.

That's not as good as the use of java.util.Random.nextInt(int n). The reasons are well documented, but i don't have them handy right now. Google for java random or something.

> Works every time, guaranteed! Have fun programming!

> Greetz, Daffn.

No. It's harder to read and produces inferior results. See above.

jverda at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 17

> > > and I know a simple answer

> >

> > A swing and a miss! ; )

> >

> > java.util.Random random = new java.util.Random();

> > int number = random.nextInt(3);

> >

> > will give an int 0,1,2

> >

> >

> http://java.sun.com/j2se/1.4.2/docs/api/java/util/Rand

>

> > om.html

>

> Why?

> int randNum = (int)(Math.random() * 3 +

> 1);

> That will give him a random number between 1 and 3.

Yeah, but the distribution sucks, realtively speaking. I'll have to dig up the docs--or you will :-) --but it's pretty well known that Random.nextInt(int n) produces better distribution, and, IMHO, it's significantly more readable.

jverda at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 18

Eum, it's me agian, why "swing and miss"? It works you know, and your way is good too, but look at the original post. He asks a number form 1 to 3 and not from 0 to 2.

Quote CaptainMorgan08:

Why?int randNum = (int)(Math.random() * 3 + 1);

That will give him a random number between 1 and 3.

End Quote.

Well, why? Simple, cos he asks for 1,2,3 and not 0,1,2

So, the best 2 ways so far for solving this problem:

int randNum = (int)(Math.random() * 3 + 1);

and

int number = (random.nextInt(3) + 1);

Daffna at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 19

> Yeah, but the distribution sucks, realtively

> speaking. I'll have to dig up the docs--or you will

> :-) --but it's pretty well known that

> Random.nextInt(int n) produces better distribution,

> and, IMHO, it's significantly more readable.

I know, I even said that I use Random.nextInt() myself. I was just trying to say that it is not wrong.

CaptainMorgan08a at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 20
How can i choose randomly one of the numbers 0,2,3 ?
x_3fPStaa at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 21

> How can i choose randomly one of the numbers 0,2,3 ?

public static int random(int[] array) {

Random r = new Random();

return array[r.nextInt(array.length)];

}

public static void main(String[ ] args) {

int[] array = {0,2,3};

for(int i = 0; i < 10; i++) {

System.out.println(random(array));

}

}

prometheuzza at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 22

int[] choices = { 0, 2, 3 };

java.util.Random rand = new java.util.Random();

int index = rand.nextInt(choices.length);

int yourRandomNumber = choices[index];

[EDIT] I'm a bit slow this morning. No surprise, really. :o)

yawmarka at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 23
> [EDIT] I'm a bit slow this morning. No surprise,> really. :o)Even slower than a Dutch guy!; )
prometheuzza at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 24
> Even slower than a Dutch guy!And that's saying something! ;o)~
yawmarka at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 25

something thats more or less(probably less) related to the original question. I jsut noticed that some of you guys use math.random. Ive never used it, but i noticed that the Random class itself is extremely... crappy. i was generating large numbers in large ranges and generating quite a few of them, and the generator seems to give me numbers that are very close alot of the times.

so i was wondering if math.random was any better than Random.

PlasmaLinka at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 26
I think Random works fairly well. Please post some code and the results you're getting and why you don't like them.
jverda at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 27
Random distribution does not mean even distribution.Flip a coin 100 times and count how many time heads come up. Will you be upset if it's not 50/50?
cotton.ma at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 28

> the generator seems to give me numbers that are very close alot of the times.

Did you know that if you put 23 people in a room, there's a greater than 50% chance that two of them have the same birthday?

> so i was wondering if math.random was any better than Random.

The Math.random() method delegates to an instance of java.util.Random, if I recall correctly.

~

yawmarka at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 29

well, i was using random to generate random speeds for different squares and different sizes for the squares and different coordinates for where the squares should start. (in an applet) and the freaken squares all moved at the same speed, where pretty much the same size, and came on the screen at the same time. No need to post code, i know its not wrong since i checked it many times, and my math is correct, because sometimes it works ok but 90% of the time, everything appears at the same time.

PlasmaLinka at 2007-7-21 10:13:10 > top of Java-index,Java Essentials,New To Java...
# 30

> No need to post code, i know its not

> wrong

Famous last words. "My code can't be wrong. It must be the tool."

Regardless of how correct you think your code is, if you won't post the code and details of what it's doing--actual numbers as well as "the squares are about the same size," then there's no way for anybody to help you. I can say with high degree of confidence, however, that just switching to Math.random won't help.

jverda at 2007-7-21 10:13:14 > top of Java-index,Java Essentials,New To Java...
# 31
> No need to post code, i know its not wrongNo, of course not. And you've the unit tests to prove this, correct?~
yawmarka at 2007-7-21 10:13:14 > top of Java-index,Java Essentials,New To Java...
# 32
> > No need to post code, i know its not wrong> > No, of course not. And you've the unit tests to prove> this, correct?No need to unit test. He knows it's not wrong.
jverda at 2007-7-21 10:13:14 > top of Java-index,Java Essentials,New To Java...
# 33
> No need to unit test. He knows it's not wrong.Oh, yes. Of course. He's far better off than me, then. Any time my code does something unexpected, I automatically assume it's because of something I've done and write tests accordingly...~
yawmarka at 2007-7-21 10:13:15 > top of Java-index,Java Essentials,New To Java...
# 34

It is possible to make a pseudo-random number number generator which misses out number, which is terribly biased or which repeats after just a few iterations, or all three. In fact it's pretty easy to write such a thing if you're doing it badly. That's why random number generators are a special interest for casual programmers, cryptography experts and hackers.

You don't need anything nearly as elaborate as a device which measures radiactive decay or background radiation from deep space to get natural random numbers. You can make such a thing with an analogue feedback circuit which generates electrical noise and then convert it to digital.

vapourmilea at 2007-7-21 10:13:15 > top of Java-index,Java Essentials,New To Java...
# 35
easy, with Math.random
ciencias_ismaela at 2007-7-21 10:13:15 > top of Java-index,Java Essentials,New To Java...
# 36
> easy, with Math.randomDon't bother reading the answers already given...
prometheuzza at 2007-7-21 10:13:15 > top of Java-index,Java Essentials,New To Java...