generating and validating random numbers

hi

i want to generate random numbers from 1 to 16. The thing is i only want them to be generated once. What i thought of doing was generate a random number and place the value in an array than, in a for () i would have a do while and if i see that the generated number is already in the array to generate another one until i have generated randomely on instance a a number between 1 and 16

this is what i did but it's not working

do

{

numDePlace = (int)(Math.random() * 16);

deDejaPlace[i] = numDePlace;

System.out.print(deDejaPlace[i]);

for(int j = 0; j < i + 1; j++)

{

System.out.println(deDejaPlace[j]);

if (deDejaPlace[j] == numDePlace && j != 0)

{

deTrouve =true;

}

}

}while(deTrouve ==true);

[1234 byte] By [wclefa] at [2007-11-26 18:13:52]
# 1
Put the numbers 1-16 into some Collection and use [url= http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#shuffle(java.util.List)]Collections.shuffle()[/url] on it. You will get a random-sorted Collection with the numbers 1-16 in it.
CaptainMorgan08a at 2007-7-9 5:47:05 > top of Java-index,Java Essentials,New To Java...
# 2
i don't want to sort the numbersi just want to generate (one time) the numbers 1 to 16 randomly
wclefa at 2007-7-9 5:47:05 > top of Java-index,Java Essentials,New To Java...
# 3
generate a number store it in an array. use a for loop to check if its already in there. if it is generate another, else just put the number into the array.-Bz
Bz_Unknowna at 2007-7-9 5:47:05 > top of Java-index,Java Essentials,New To Java...
# 4
> i don't want to sort the numbersYou aren't, you're shuffling them. You place 1-16 (only one of each) into the Collection and shuffle it. You can then pull them out in order.
CaptainMorgan08a at 2007-7-9 5:47:05 > top of Java-index,Java Essentials,New To Java...
# 5
thx you it worksjust to know can i do the same thing with an array of 2 dimension ?
wclefa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 6

> thx you it works

> just to know can i do the same thing with an array of

> 2 dimension ?

Why 2 dimension?

If you do not want to use java.util API class, try this:

public class UniqueRandomNumbers{

public static void main(String[] args){

int magn = 16;

int[] randoms;

if (args.length > 0){

magn = Integer.parseInt(args[0]);

}

randoms = uniqRans(magn);

for (int r : randoms){ // for (int i = 0; i < randoms.length; ++i)

System.out.println(r); // System.out.println(randoms[i]);

}

}

public static int[] uniqRans(int magnitude){

int n, ran;

int[] rans, check;

n = 0;

check = new int[magnitude];

rans = new int[magnitude];

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

check[i] = 0;

}

while (true){

++n;

if (n > magnitude){

break;

}

ran = (int)(Math.random() * magnitude) + 1;

while (check[ran - 1] != 0){

++ran; // choose next value

if (ran > magnitude){

ran = 1;

}

}

check[ran - 1] = rans[n - 1] = ran;

}

return rans;

}

}

hiwaa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 7

ran = (int)(Math.random() * magnitude) + 1;

while (check[ran - 1] != 0){

++ran; // choose next value

if (ran > magnitude){

ran = 1;

}

Not very random, is it?

ejpa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 8
> Not very random, is it?No, but it is fairly usable for number of purposes.
hiwaa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 9
Non-random purposes? Sequential purposes maybe?
ejpa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 10

You should note that the very idea of 'unique random number' in a finite range is not random at all. But sometimes we need it when we have to make a random sequence, or order, from a finite number of elements, e.g. random match-making. Try my humble program several times and see the results -- be aware that each time of the loop iteration, *the starting number* is randomly generated.

hiwaa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 11
> You should note that the very idea of 'unique random> number' in a finite range is not random at all.Why?Humble solution is not as random as reply #1.
ejpa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 12

> > You should note that the very idea of 'unique

> random

> > number' in a finite range is not random at all.

>

> Why?

Because the width of the choice gradually narrows as we go picking up the element from the randomized sequence. At its extreme, the last element is definitive before we see it.

>

> Humble solution is not as random as reply #1.

Well, I don't see major practical difference between two algorithms, mine and Collections.shuffle() one. I am not a math guru, though. I hope JoASH would give help in this regard.

hiwaa at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 13

@hiwa: What's the use of the 'infinite loop'?

while (true){

++n;

if (n > magnitude){

break;

}

}

Isn't this just the same as while (n <= magnitude) {

++n;

}

or just a for loop?

Peetzorea at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 14

I think ejp has a problem with the statements inside your innermost while loop. Why don't you just generate another random again?

I don't have issues with your algorithm. It's random and it's a little faster than using a for loop for iterating over an array to see if the last generated number was already generated before.

Peetzorea at 2007-7-9 5:47:06 > top of Java-index,Java Essentials,New To Java...
# 15

> Isn't this just the same as

>

>> while (n <= magnitude) {

>++n;

> }

Yes. You are right.

> Why don't you just generate another random again?

It can be infinite, theoretically :)

Message was edited by:

hiwa

hiwaa at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...
# 16

> > > You should note that the very idea of 'unique

> > random

> > > number' in a finite range is not random at all.

> >

> > Why?

> Because the width of the choice gradually narrows as we go picking up the

> element from the randomized sequence. At its extreme, the last element is

> definitive before we see it.

What is sought in the 'unique random number' problems is not so much

random numbers as random orderings.

For instance if you take a finite range 1 .. n there are n! different ways of

shuffling the set. What is being looked for is a process that, each time it

is run, will produce one of these n! orderings in some unpredictable way.

pbrockway2a at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...
# 17

> > > You should note that the very idea of 'unique random

> > > number' in a finite range is not random at all.

> > Why?

> Because the width of the choice gradually narrows as

> we go picking up the element from the randomized

> sequence. At its extreme, the last element is

> definitive before we see it.

The choice does indeed narrow, but at each step we do make a random choice. What's not random about that? Even the last choice is indeed random, because had we started out differently, another element could have been the last one to be picked...

duckbilla at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...
# 18

hi this is a simple way. but if you want to genetate 100 no this is not sutable.beause it will waste processing time.

public class Validation

{

public static void main(String args[])

{

int no[]=new int[16];

int count=1;

boolean chk=true;

while(count <= 16)

{

int val=(int) Math.ceil(Math.random()*16);

chk=true;

for(int i=0;i<count;i++)

{

if(no==val)

{

chk=false;

break;

}

}

if(chk==true)

{

no[count-1]=val;

count++;

}

}

for(int i=0;i<16;i++)

{

System.out.print(no+",");

}

}

}>

jeyrama at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...
# 19

hi this is a simple way. but if you want to genetate 100 no this is not sutable.beause it will waste processing time.

public class Validation

{

public static void main(String args[])

{

int no[]=new int[16];

int count=1;

boolean chk=true;

while(count <= 16)

{

int val=(int) Math.ceil(Math.random()*16);

chk=true;

for(int i=0;i<count;i++)

{

if(no==val)

{

chk=false;

break;

}

}

if(chk==true)

{

no[count-1]=val;

count++;

}

}

for(int i=0;i<16;i++)

{

System.out.print(no+",");

}

}

}>

jeyrama at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...
# 20

> What is sought in the 'unique random number' problems is not so much

> random numbers as random orderings.

It often implies concept of ordering in a real-world usage.

> Even the last choice is indeed random

From your context of things, it may be right. I can understand that. But it is not the same context nor same usage view as mine. Don't introduce different context arbitrarily, please.

hiwaa at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...
# 21
>But it is not the same context nor same usage view as mineCare to explain what your context is like then?
duckbilla at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...
# 22
> >But it is not the same context nor same usage view> as mine> > Care to explain what your context is like then?It's a common use-case scenario. I think you could infer that from reply #12.
hiwaa at 2007-7-21 17:19:06 > top of Java-index,Java Essentials,New To Java...