I am trying extract 5 numbers from a loop that generates 78 numbers randomly. How would i do that?

for(i=0; i<=78; i++)
{
//i am not sure how to extract the numbers from here randomly
}

//I am also wondering how to take it's average, or am i doing it right?
System.out.println(i/3);

Google was not my friend for me on this project (it is a small portion of an assignment), i couldn't get a solution... unless i google'd wrong.

hai MICHAEL,

colud you explain me clearly what you are going to do
whether you want 5 random numbers between 0 to 78 or something else

can you make me clear on this requirement

so that we are here to help you

happy coding

Edited 3 Years Ago by radhakrishna.p: getting requirement information to give solution

Your loop has 79 iterations, not 78, so you might be wrong about how many numbers it generates. I will just call the total count of generated numbers NUMS.

As you go through the loop keep of how many numbers you still need to extract. I will call that number n. Each time you randomly extract a number, n goes down by one. If you want to evenly distribute your randomly extracted numbers, then in each iteration of the loop the odds that you will extract the current number are: n / NUMS - i. This ends up giving each number a 5 / NUMS chance of being chosen, but as we go through the loop we need to correct the odds to take into account the numbers that have already been chosen and the numbers that have been skipped.

In other words, in each iteration, the odds of choosing the current value are:
(number of values still needed) / (number of values left to choose from)

Edited 3 Years Ago by bguild

@radhakrishna.p, i am trying to make a loop that counts to a specific number (in this case 78) and extract 3 random numbers from it. Then find the average (i think i can probably figure that part out, just not the first 2 steps).

@bguild, oh okay, i will practice your solution.

Not tested and new to Java but would this serve your purpose?

int Sum = 0;
int i = 0;

while (i != 3) {
    Sum += (int)(Math.random() * 79);
    ++i;
}

float Average = Sum/i;

System.out.println("Average = " + Average);

there, you don't have the separate numbers, so what if you want to perform other operations on them?
create an array of x ints, and each iteration, save the value to myArray[i];

In other words, in each iteration, the odds of choosing the current value are:
(number of values still needed) / (number of values left to choose from)

so if i have to find 'k' non reapeating numbers from a certain 'range' , and if i do this:

((obtained random num).(odds).(range)) + min

will this be good?

also, if it does give good results, then that would mean the results are in uniform distribution, so calculating standard deviation over each sample should give a high value right ? my apologies if im being vauge, i havent touched statistics in over four years.. gone quite rusty..

Edited 3 Years Ago by somjit{}

There are many ways to generate unique random numbers.I am discussing two of them below:-

  • Write the numbers sequentially in a list structure.
  • Shuffle it.
  • Take the first 'n'.
  • This uses only a single Random instance.

Another option

Call Random rand = new Random();rand.nextInt(78)
and store value in Set.If added successfully(means unique as set doesnot support duplicacy) means it is unique.
Check the size,if it is less than 3 then keep on adding.

For taking average,just add the elements of list/set and divide by its size,i.e,3 in your case

Edited 3 Years Ago by IIM

and store value in Set.If added successfully(means unique as set doesnot support duplicacy) means it is unique.

that sounds good... but what about the expense of storing the data in the set... ? if that isnt too expensive, then i guess its going to be a pretty good solution.
i talk about expense here coz i have a feeling that the Original poster wanted to use that particular method to reduce memory requirement from O(N) to O(K) , where N = total population, ie 78 , and K = sample size , ie 5 .

Original poster wanted to use that particular method to reduce memory requirement from O(N) to O(K) , where N = total population, ie 78 , and K = sample size , ie 5 .

As per i can see original poster has asked for a way to find random numbers.Memory for set option is O(n) as it is storing only till the number of random numbers required.Although it will have more time complexity.

As for first option of storing in list.It will require O(k) where k is the random number range size.So more memory is required for this case.

((obtained random num).(odds).(range)) + min

will this be good?

I can't figure out what that means, but I feel confident in saying no, it will not be good. It naturally depends on where you get your random number and how you want to use the number that you calculate using that formula, but I can't see how it could be useful for any purpose.

I am curious to know the origin of that formula. It's possible that I am misjudging it just because I can't guess how it should be used.

Edited 3 Years Ago by bguild

In other words, in each iteration, the odds of choosing the current value are:
(number of values still needed) / (number of values left to choose from)

i took it from there....
if:

randomNum = random.nextInt()
odds = (number of values still needed) / (number of values left to choose from)
range being the range from which the samples have to be taken

then what i meant was that will

( odds x randomNum x range ) + min

this formula above , iterated over K times , give K sample units , such that no two sample units are the same?

im saying about this, because i feel that if this is possible, then the OP can put the range as 78 , and then keep a count , and when the count matches any of the numbers generated through this sampling process , he keeps that particular value , else disregards n and iterates again...

Edited 3 Years Ago by somjit{}

try this

    int values[]=new int[3];
    float sum=0;
    for(int i=0;i<3;i++)
    {
    values[i]=(int)(Math.random()*78);
    System.out.println("random number="+values[i]);
    sum+=values[i];
    }
    System.out.print("Average="+(sum/3));

hope this helps you to solve the issue . . .

@IIM
thanks for pointing out. i will try to post updated code

and at last the thread creator didnot specify this requirement

Edited 3 Years Ago by rishif2

this will not return unique random number.

Yes. i read recently that choosing a random number from an entire array wont give you correct results. which is what you said above as well.. knuth shuffle works around this problem by choosing randomly between 0 and i , or in some cases (n-1) to i , but not from 0 to (n-1).

edit : found this to be an interesting read.

Edited 3 Years Ago by somjit{}

sorry for not returning to this thread you guys, it wasn't indicated as un read so i basically skipped over it but anyways...

I was using rishif's solution and it was working properly but i am not sure what you (IIM) by it not returning a unique random number. It returns a unique number everytime i run it. Am i missing something, or was this code updated before? But anyways, thanks you guys i didn't realize how many replies i gotten on this thread.

@michael : you see , apparantly , the results will look like they are giving random numbers , but lets go through a simple case:

suppose you have 3 cards from a deck , so number of diff permutations will be 3! = 6 , lets see this in a bit detail :

'__________' '_________' '_________'

the above are 3 slots, where you can put the cards.

for the 1st slot , you have 3 options.
for the 2st slot , you have 2 options left.
for the 3st slot , you have 1 option left.

thus 3*2*1 = 6 total number of ways you can arrange the cards.

if you want to do this in code , you will have to reduce the population from where your taking the cards by 1 in every turn , thus resulting in 2 options down from 3 , and then 1 option down from 2 in the 2nd and 3rd runs.

if you dont do this , and take a random number from the whole population , ie 3 on each turn, you would get 3*3*3 = 27 combinations , which isnt correct, and will give you biased results. You can check that with the naked eye as well as the result obtained here(27) isnt divisible by 6 ( the correct result ) . So some arrangements will turn up a greater number of times than others.

i had posted a link on one of my earlier replies... there you will find a more detailed explanation of what i said above along with charts and diagrams. i found that a very good read.

Edited 3 Years Ago by somjit{}

oh okay, i see where you get this from. I will try to tweak the code and see if i can get it to work out... if not i will ask the question :)

Thanks :)

This question has already been answered. Start a new discussion instead.