Sign in to follow this  

Life time of a random number generator

Recommended Posts

Does the life time of a random number generator (in Java) extend to and including when a system shuts down and then start up again?

	    Random rand = new Random();
	    n = rand.nextInt((max - min) + 1) + min;

 

What I mean is for example if max = 9 and min = 0.  I would expect that if I run 10 times without exiting to program (because I can run a section of the code without exiting by pressing a UI button), numbers would be randomly generated without any repeat of the same number until after the 10th number has been generated.

But i would also like it to run this way even if I exit the program. So if for instance I exit the program after the 3rd run, when I restart I expect that there would be a memory of the runs before the previous exit and the next run after the restart will be regarded as the the 4th run. This would ensure that there would be no repeats of numbers generated in the previous runs until after the 10th run.

Does it run this way? If not what algorithm can I add to ensure the code runs this way?

Share this post


Link to post
Share on other sites

No, that's not how random numbers work.

If you pick a number at random between 1 and 10 then each number has a ~10% probability of being picked, including the last number that was picked, and it certainly won't "remember" numbers in each different run of the program. 

If you want to ensure each number is different, then you need to either run the RNG again until you get a different number, or change the range each time (which has it's own problems).

What are you trying to achieve here? Describe the problem not the solution.

Share this post


Link to post
Share on other sites

I'm not sure what gave you the impression that a random number generator would do incremental numbering, but okay.  No, you could get the number 4, 3 times in a row.  Hence why it's random.  You should really learn to find and read documentation instead of waiting, possibly days, for a reply on these forums.  Or make simple and quick test programs that run the code and outputs the results to see what it does.

http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

And no, it will not save from run to run.  But since it takes a seed, if you use the same one, you should get the same results each run.  But since all you're trying to do is add one to a number, why not just make a variable, save it out to a file, read it back in each time you run and just increment it?  You can do simple logic for when you reach the max size, wrap it back around to the minimum size.

Share this post


Link to post
Share on other sites

The random number generator will be freed when the program exits, so if you want to maintain its progress you'll need to somehow serialize its state.  For some RNGs that can be a lot of data.  I have no idea how Java's built in RNG works, but I expect it's something basic like a LCG, in which case it shouldn't be too much to store.  I doubt it's a serializable class though, so you'll want to figure out how to express that yourself.

 

However, as others have said above, RNGs do not guarantee "no repeats".  You need extra mechanisms for that.

Share this post


Link to post
Share on other sites

[Sorry if my grammar is screwed up, i've suddenly been in a rush, will come back and edit this later] 

 

What are you trying to achieve here? Describe the problem not the solution.

 

You will be surprised that the problem that drove me to this is client server socket programming.

To summarise I send and receive image files and string data successfully (?) The question marks is because without changing the code between runs it blocks at the server with a success rate of just about approx 30%. That means 70% of the time it blocks. After a lot of hard work I found nothing else improved the situation until I change the outputstream object to be different from the previous one. Then the non-blocking success rate jumped to ~80%

The success rate needs to be 100% though. I could easily always start with zero but it may surprise you - when I restart the server it blocks because it has a memory of the a previous outputstream object

So one way is to write the previous  output number to a file and read it at the start so it can be avoided, but i only want to do this as a last resort. Using RGN is my first choice, if it could work the way I described originally (it would be a much simpler solution) because if I choose a very large number for 'total' in the code below, chances are that the player would never reach that number even when they exit and restart the game

                  Random rand = new Random();
	          n = rand.nextInt(((total-1) - 0) + 1) + 0;
                   .......
                   .......
                   .......
                  OutputStream outps[];
                  outps = new OutputStream[total];
                   
                  if(n>=total) n=0;
                  outps[n] = sock.getOutputStream();
    
                  outps[n] = sock.getOutputStream();
                  outps[n].write(mybytearray,0,mybytearray.length); // send file
        
                  outps[n].flush(); 

I'm not sure what gave you the impression that a random number generator would do incremental numbering,

 

I think you misunderstood me. That I was hoping for non repeating generation didn't mean I expect the randomly generated numbers to be incremental like n, n+1, n+2...

 

The random number generator will be freed when the program exits, so if you want to maintain its progress you'll need to somehow serialize its state.

 

In that case May be I will have to go by my second option of saving to file before exit and reading from file at the start to overcome the socket blocking problem 

 

Share this post


Link to post
Share on other sites
Posted (edited)
You will be surprised that the problem that drove me to this is client server socket programming.

To summarise I send and receive image files and string data successfully (?) The question marks is because without changing the code between runs it blocks at the server with a success rate of just about approx 30%. That means 70% of the time it blocks. After a lot of hard work I found nothing else improved the situation until I change the outputstream object to be different from the previous one. Then the non-blocking success rate jumped to ~80%

he success rate needs to be 100% though. I could easily always start with zero but it may surprise you - when I restart the server it blocks because it has a memory of the a previous outputstream object

So one way is to write the previous  output number to a file and read it at the start so it can be avoided, but i only want to do this as a last resort. Using RGN is my first choice, if it could work the way I described originally (it would be a much simpler solution) because if I choose a very large number for 'total' in the code below, chances are that the player would never reach that number even when they exit and restart the game

 

 

So the server is sending data to the client and blocking over the network?  Does Java not support an asynchronous networking API?  I'm not familiar enough anymore with Java to give you an actual solution, but I can all but guarantee that there's a way to do what you need without having to suffer that horrid abomination you're working towards.  You should never need to play musical chairs with your sockets.

Edited by SeraphLance

Share this post


Link to post
Share on other sites
So the server is sending data to the client and blocking over the network?  Does Java not support an asynchronous networking API?  I'm not familiar enough anymore with Java to give you an actual solution, but I can all but guarantee that there's a way to do what you need without having to suffer that horrid abomination you're working towards.  You should never need to play musical chairs with your sockets.

 

Of course I am sure experts wouldn't solve the problem this way. I am very sure there are more elegant ways of solving this. I am a beginner in socket programming so I need to start with a less complex linear client-server system first (which is what i'm doing) and when I get to be a master of that, i would go for multiple sends and receives data/file system and more complex solutions.  

I have posted several posts on my socket coding issue. I got  a lot of HELP (many thanks to those guys). But I'm also sure I had become a nuisance in the subforum. More so despite that I admit that I'm a beginner in the field I sometimes get answers described in so high level way that I have no clues what is being described

But the good news for me is that my hard-hoc solution improved the non-blocking rate from ~30% to~80% and I'm pretty sure that if i can avoid using the same outputstream object twice, i.e. make sure current stream objects is a completely fresh one, even from previous outputstream objects before previous program exits, I will successfully nail the remaining 20% block rate

Anyway looks like I will have to write-read from file to keep track of new(ed) objects. Solution looks really bizarre but its what the result-data indicate works

Share this post


Link to post
Share on other sites

What I mean is for example if max = 9 and min = 0.  I would expect that if I run 10 times without exiting to program (because I can run a section of the code without exiting by pressing a UI button), numbers would be randomly generated without any repeat of the same number until after the 10th number has been generated.
 

Does it run this way? If not what algorithm can I add to ensure the code runs this way?

To achieve what you are describing here use a list or array containing the numbers. For example:

possibilityList[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Now you randomly remove one of the numbers.

possibilityList.remove(random(possibilityList.size()))

The result will be the list with one item removed. This is the same as pulling a raffle from a basket.

 

Like the others pointed out this isn't how chance works. In theory if a player has a 10% chance of doing a action it is possible, with a true random function, that they can never achieve the action. It's also possible that even if a players has 0.00000000001% of doing some thing that they could get lucky and get it on each try.

Share this post


Link to post
Share on other sites

 I doubt it's a serializable class though, so you'll want to figure out how to express that yourself.

Actually, it is serializable. See [b]@[member='Mike2343'][/b]'s link.

But that's not a good solution to this problem.

Share this post


Link to post
Share on other sites
Posted (edited)

I would say it too much trouble to solve the problem in that way. It might sound harsh, but I would advice you to drop the solution you have been implementing and use Java NIO which was developed exactly for the purpose of non-blocking IO. Java NIO might look weird at the beginning, but once you get you will realize much powerful it is and how much trouble you avoided (in comparison to the custom-made solution). This tutorial has source code at the bottom of the page and detailed explanation how you can make it work.

 

Additionally, you need to remember that running a server on a single thread (you have not mentioned this, but I throw it as a warning) will always block multiple users to perform concurrent operations.

Edited by NuclearPixels

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this