#How do I make random number generation more random?

1 messages · Page 1 of 1 (latest)

civic lynx
#

Hello! So I’ve been trying to write a program for coupon collector program in case of six faced die.
I have attached pic of my code + pasted my code below. Lemme know if you have any idea!

import java.util.Random;
import java.util.Arrays;

class die{

public static void main(String[] arg){

Random rand = new Random();

int[] T = new int[2];
T[0] = 1+rand.nextInt(6);
int i = 1;
int j = 1;

        do{
             int h = 0;
             T[i] = 1+rand.nextInt(6);
             
             for(int k=0; k<i; k++) { if(T[i] != T[i-k]){h++;} }
             if(h==i){j++;}  
            
              
              i++;

              int[] temp = new int[i+1];
              temp=T;

              T = new int[i+1];
              T=temp;                      }while(j!=6);

System.out.print(i);


}}```
spark pendantBOT
# civic lynx Hello! So I’ve been trying to write a program for coupon collector program in ca...

Detected code, here are some useful tools:

Formatted code
import java.util.Random;
import java.util.Arrays;

class die {
  public static void main(String[] arg) {
    Random rand = new Random();
    int [] T = new int [2] ;
    T[0]  = 1 + rand.nextInt(6);
    int i = 1;
    int j = 1;
    do {
      int h = 0;
      T[i]  = 1 + rand.nextInt(6);
      for (int k = 0; k < i; k++) {
        if (T[i]  != T[i - k] ) {
          h++;
        }
      }
      if (h == i) {
        j++;
      }
      i++;
      int [] temp = new int [i + 1] ;
      temp = T;
      T = new int [i + 1] ;
      T = temp;
    }
    while (j != 6);
    System.out.print(i);
  }
}
#

<@&987246883653156906> please have a look, thanks.

civic lynx
#

im kinda more interested in why this code doesn't work rather than looking at the optimized one

#

actually screw this

#

im using lists

#

so much better

#

:nozoomi:

mortal girder
#

What are you trying to do

#

Wait nvm

#

Nvm what are you trying to do

civic lynx
#

I got it btw

#
import java.util.Random;
import java.util.function.Supplier;

class CC{
public static void main(String[] arg){
  
  int k = Integer.parseInt(arg[0]); //Number of coupons to be collected
 
  Random rand = new Random();
  List<Integer> T = new ArrayList<Integer>(); //Coupon Collector
  
  int i = 1;
  int j = 1;
  
  T.add(1+rand.nextInt(k));

                                do{  int x = 1+rand.nextInt(k);
                                    
                                     if(T.contains(x)){ }
                                     else{ T.add(x); j++; }

                                     i++;                       }
                                
                                while(j!=k);

  System.out.print(i);

}}
spark pendantBOT
# civic lynx ```import java.util.*; import java.util.Random; import java.util.function.Suppl...

Detected code, here are some useful tools:

Formatted code
import java.util. * ;
import java.util.Random;
import java.util.function.Supplier;

class CC {
  public static void main(String[] arg) {
    int k = Integer.parseInt(arg[0] );
    //Number of coupons to be collected
    Random rand = new Random();
    List<Integer> T = new ArrayList<Integer>();
    //Coupon Collector
    int i = 1;
    int j = 1;
    T.add(1 + rand.nextInt(k));
    do {
      int x = 1 + rand.nextInt(k);
      if (T.contains(x)) {
      }
      else {
        T.add(x);
        j++;
      }
      i++;
    }
    while (j != k);
    System.out.print(i);
  }
}
civic lynx
#

This simulates CC for k coupons

civic lynx
#

Okay this is weird

#

I checked my program pretty thoroughly so I don't think there are any mistakes

#

But I have problem with the degree of randomness Random() offers

#

This simulation does give the correct expected value of Time(random variable) for coupon collector problem(CC)

#

But the averages are too small compared to real life

#

What can I do to improve the degree of randomness ?

#

How do I make random number generation more random?

civic lynx
hidden halo
#

The randomness of the default isn't terrible. SecureRandom can be used for a cryptographically strong source of randomness - but it's comparatively expensive.

#

You can simplify the code to Find the first k unique values.

  1. use a Set.
  2. loop while the Set size is less than the target k adding a random value to the set (duplicates will be ignored for you).
  3. Use ThreadLocalRandom.current() so you don't have to create a random to use.

If you care about the order they were encountered you can add successful checks to a list (or use LinkedHashMap to collect the matches) - or do it in a method that takes a consumer so you can decide later.

civic lynx
#

👍 will try that

#

Will have to look all those up peepoG

civic lynx
#

I was thinking of I could using smthg like this

#

And integrate it in my code

#

Tho I dunno much about interface and stuff :P

uneven salmon
#

Built in random is random enough for what you are doing.

#

Are you just trying to make a histogram of random numbers?

worldly bone
#

also use ThreadLocalRandom instead

uneven salmon
#

meh... leave an option for that by making the random a parameter. That way when you do testing you can chose a fixed seed.

worldly bone
#

ok yeah if thats needed

civic lynx
#

And I noticed these averages are smaller compared to when I actually try to do this irl

#

Which is weird

molten pebble
#

@civic lynx

#

you don't need any of that

#

just use what you had

#

Random is good enough so it's impossible for human to see any pattern

#

if you see a pattern, either your perception is biased, or the problem is in your code

molten pebble
#

here it should average to 3

#

in a long run

civic lynx
#

for 6 sided die ?

#

for 6 sided die the expected stopping time is 6H_6

#

About 14.7

molten pebble
#

wdym

civic lynx
#

The average

#

By law of large numbers the avg in our case converges to expected value

#

when i say expected value I’m talking about expectation of our underlying time random variable

#

but we don’t need to get into detail

#

So i wrote a code to find this avg

#
import java.util.Random;
import java.util.function.Supplier;

class avgCC {

 public static int CC(int y) {
  
  Random rand = new Random();
  List<Integer> T = new ArrayList<Integer>(); //Coupon Collector
  
  int i = 1;
  int j = 1;
  
  T.add(1+rand.nextInt(y));

                                do{  int x = 1+rand.nextInt(y);
                                    
                                     if(T.contains(x)){ }
                                     else{ T.add(x); j++; }

                                     i++;                       }
                                
                                while(j!=y);

  return i;         }

 public static void main(String[] arg) {
              
           int k = Integer.parseInt(arg[0]);
           int N = Integer.parseInt(arg[1]);
           float avg=0;
     for(int m=1; m<N; m++){ avg = avg + (float)CC(k)/(float)N; };
    
     System.out.print(avg);
} }
spark pendantBOT
# civic lynx ```import java.util.*; import java.util.Random; import java.util.function.Suppl...

Detected code, here are some useful tools:

Formatted code
import java.util. * ;
import java.util.Random;
import java.util.function.Supplier;

class avgCC {
  public static int CC(int y) {
    Random rand = new Random();
    List<Integer> T = new ArrayList<Integer>();
    //Coupon Collector
    int i = 1;
    int j = 1;
    T.add(1 + rand.nextInt(y));
    do {
      int x = 1 + rand.nextInt(y);
      if (T.contains(x)) {
      }
      else {
        T.add(x);
        j++;
      }
      i++;
    }
    while (j != y);
    return i;
  }
  public static void main(String[] arg) {
    int k = Integer.parseInt(arg[0] );
    int N = Integer.parseInt(arg[1] );
    float avg = 0;
    for (int m = 1; m < N; m++) {
      avg = avg + (float ) CC(k) / (float ) N;
    };
    System.out.print(avg);
  }
}
civic lynx
#

This does converge for k=6 and large N to 14.7 (on average)

#

but for lower values of N the avg tend to be much smaller than what is observed irl

molten pebble
#

see, it works

civic lynx
#

My random variable is different

#

You are avging the x themselves

#

Which will tend to 3.5

#

bcuz x is uniformly distributed

civic lynx
molten pebble
civic lynx
#

So basically

molten pebble
civic lynx
#

that is if you sample it large number of times

civic lynx
#

And takes their mean

#

so for k=6 this is asks how many times you have to roll a die so that you see all the faces

#

and i do this process N times

#

Then I take the mean

#

That’s basically what the avgCC program is doing

civic lynx
#

even for N=10

#

the avg given by program is a bit small

civic lynx
#

so this makes me wonder if it’s my code or the random()

molten pebble
molten pebble
#

and did you calculate the probablility of N ?

civic lynx
#

But the program gives me 11-14

#

Always

civic lynx
molten pebble
civic lynx
#

N is a free variable

#

Do you mean i?

molten pebble
#

ah

#

I mean

#

the average

molten pebble
civic lynx
#

oh

#

The avg so that should be very high

#

bcuz expected value is 14.7

#

And by LLN avgs should go to this value

#

For large N

molten pebble
#

how do you know that ?

civic lynx
#

You can prove it mathematically

#

Look up law of large numbers

molten pebble
#

also, did you try to reuse the Random object ?

civic lynx
#

(Im kinda new to java)

molten pebble
#

yes

civic lynx
#

Yeah

#

I used it repeatedly to get random values between 1 to 6

molten pebble
#

I indeed get 14.7

molten pebble
civic lynx
civic lynx
#

I’m calling the same random object repeatedly

#

rand

#

So no new random objects are getting created

molten pebble
#

yea

#

you shouldn't do that

#

but

civic lynx
#

O

#

Wouldn’t it give me new random number everytime tho?

#

I mean it does give me new integer everytime

molten pebble
#
int CC(RandomGenerator rand, int y) {
        Set<Integer> T = new TreeSet<Integer>();
        //Coupon Collector
        int i = 1;
        int j = 1;
        T.add(1 + rand.nextInt(y));
        do {
              int x = 1 + rand.nextInt(y);
              if (T.contains(x)) {
              }
              else {
                    T.add(x);
                    j++;
              }
              i++;
        }
        while (j != y);
        return i;
  }
void run(int k, int N, RandomGenerator rand) {
  long sum = 0;
  for (int m = 0; m < N; m++) {
    sum += CC(rand, k);
  };
  System.out.print(sum/(double)N);
}
run(6, 100000)
#

anyway

#

@civic lynx what's the problem already ? because personally I find 14.7

civic lynx
#

Run this repeatedly for N= 10

#

You get avgs in range 10 to 16

#

and irl you get avgs in range 17 to 25

#

Of course they will match for large values of N (that is in limit)

#

its the small values that are problematic

molten pebble
civic lynx
#

N is free variable bro

#

N is the number of time you wanna do this process of finding out the number of tosses to see all sides of die

molten pebble
#

i am talking about avg

civic lynx
#

Yeah

#

It is avg over the N values of T

molten pebble
#

what is the probability of having avg between 11 and 14 for small values of n ?

civic lynx
#

lets see hmm

molten pebble
#

Because random doesn't lie

civic lynx
#

All hail random() alienguy

molten pebble
#

I tried different random algo and they seem to all give of the same results

civic lynx
#

OOOF

#

hm

#

so maybe the rolls i did were skewed ?

#

But i rolled the online dies

molten pebble
#

or maybe your formula is wrong

civic lynx
#

Which formula

molten pebble
#

14.7

civic lynx
#

Nah thats correct

#

Its a well known problem

#

coupon collector

molten pebble
#

it's only 14.7 for large n right ?

civic lynx
#

Yeah

#

but we have the following

#

Do we have latex here

#

$test$

molten pebble
#

== we have this thing

#

maybe

civic lynx
molten pebble
#

how does it work

#

idk

#

the thing with this problem

civic lynx
#

Lemme just photo

molten pebble
#

is that it tends to 14.7 right

#

but

#

it's not your regular average

#

because it doesn't do 14 then 18 then 12 then etc

civic lynx
#

so the probability is only considerably small when

#

c>13

molten pebble
#

it's rather that most results are under 14.7 and only when you take them all, it gives something like 14, 13, 12.5, 14.7, 16.8, etc and in the long run, it will average with 14.7 but there are little average that are more than 14.7

#

maybe you could try to do a median

civic lynx
#

But the usual avgs should work

#

LLN holds

molten pebble
civic lynx
#

T is the avg

#

Or rather

#

Value of time T

#

But huh we want avg

#

Just a sec

#

Yeah so there N to

#

So we have

#

Let AVG be the avg random variable

spark pendantBOT
molten pebble
#

alright

#

it works

civic lynx
#

epsilon our error

molten pebble
civic lynx
#

Yeah so the thing you asked

#

AVG_10 being avg when N=10

#

So yeah its quite less

#

And it is confirmed by the irl tosses i did

#

Avgs lied between 17 and 25

#

So there must mistake in code or the random()

molten pebble
civic lynx
#

Oh wait i mean

#

oh fuck

#

Its the probability of AVG_10 being greater than 18.7 or less than 10.7

#

which is less than 0.37

#

huh

#

so the only thing faulty was the tosses i did IRL peepoW peepoW peepoW

molten pebble
civic lynx
#

Okay

civic lynx
#

Probably I'm missing some stuff

#

But yeah I don't think we need to dig this anu further

#

random() works

#

🙏

spark pendantBOT
#

@civic lynx

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

civic lynx
#

Okay brotha

odd rivet