Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Patterned returns with Math.random() (http://www.programmingforums.org/showthread.php?t=15294)

dimlyBright Mar 1st, 2008 12:25 AM

Patterned returns with Math.random()
 
1 Attachment(s)
I hope I am doing this correct. I am having trouble with a little program I made. It generates lottery numbers in an unconventional way. It is random numbers but the main five numbers repeat exactly every eleventh line. What I mean is, if you give it a command line argument greater than 11 you will see the supposedly randomly generated lines begin to repeat themselves. It is completely bizarre. I suppose that if you want to comment on how bad my coding is you are free to do that as well. It will only help me code better in the future. Please, if anyone can help, it is like a splinter in my mind.
Attachment 625

Sane Mar 1st, 2008 9:15 AM

Re: Patterned returns with Math.random()
 
You can research this by Googling it. I found a site to start you off.

http://www.particle.kth.se/~lindsey/...aRandNums.html

Quote:

Random number generator formulas actually produce a sequence of numbers that eventually repeat. For the same seed value a formula will produce the same sequence. A seed simply selects where in the sequence to start. The generator will eventually repeat that seed value and start the same sequence again.
Search for similar terms, and use of the setSeed function in Java, to learn more.

Alias Mar 1st, 2008 11:08 AM

Re: Patterned returns with Math.random()
 
Yeah Sane, but thats a pretty bad generator implementation, or something else is going wrong if the repeat beings to occur so soon, you must admit.

Alias

dimlyBright Mar 1st, 2008 12:16 PM

Re: Patterned returns with Math.random()
 
yes, each number in the set {random number 1 - 56, random number 1 - 56, random number 1 - 56, random number 1 - 56, random number 1 - 56} is repeated exactly every 11th set generation however the power ball is always different! That means that it only takes 11*5 random generations before exact repeats occur. I say that because the power ball is generated using a different class entirely and it seems to be unaffected by this weirdness.

OpenLoop Mar 1st, 2008 12:18 PM

Re: Patterned returns with Math.random()
 
Usually random() functions can accept seeds (C#, C++ support this, I assume Java does this too), different seeds give different random lists. If you seed the function with Time, you will get a truly random list.

Alias Mar 1st, 2008 12:22 PM

Re: Patterned returns with Math.random()
 
Personally I would encapsulate the RNG in a class of its own in which it should be constantly generating numbers by the second, then use a method to get the latest generated number - not exactly a solution to your problem but certainly a more appropriate way of returning seemingly random numbers.

Alias

Alias Mar 1st, 2008 12:25 PM

Re: Patterned returns with Math.random()
 
Sorry to smash your glass bubble OpenLoop, there is no truly random number, or set of numbers generated by either C# or Java implementations of RNG's.

Alias

dimlyBright Mar 1st, 2008 3:11 PM

Re: Patterned returns with Math.random()
 
I don't think I need a "truly random" generator. I am not looking to get thousands of unique sets just <=20. I am stuck with 11 unique sets now (not including the power ball)

let me describe my madness. The file src.zip is my program given in the original post. Imagine you selected a random number from a bin I will call 'a', looked at it and then returned it to 'a'. You wrote the number on a card and placed that card in a bin I will call 'b'. Again select randomly from bin 'a', write it on a card and return it. Now compare it to every card in 'b'. If it is the same as another card in 'b', destroy it and repeat the drawing process again from 'a'. Do this process over and over until you have numbers in 'b' equal to one less than the numbers in 'a'. Whatever number is missing from 'b' is the final product! That is the first lotto number you use in the selection process. for every number made this way you place it in an array and call a recursive algorithm to compare every new number to every preexisting element in the set. Do this five times with 'a' and 'b' and then a unique sixth number is made using numbers 1-42 from new sets 'c' and 'd' for the power ball number.

Why? WHY!? Well because I am resigned to believe that I will never select even one number that matches the lotto numbers so this way the numbers given are all numbers that were never selected! MWAHAHAHAHAHAAAA!!!!!

ahem... sorry. the program compiles and just requires an argument. If you fail to give an argument, fear not for it will ask for and input.

Sane Mar 1st, 2008 3:33 PM

Re: Patterned returns with Math.random()
 
The answer is within your reach. Do some searching on the terms given in this thread. Particularly, seeding the random generator. It sounds like you keep seeding the random number generator with bad values, or the same values. Compare your code against that of what you can find through several different sources (IE Google).

dimlyBright Mar 1st, 2008 11:35 PM

Re: Patterned returns with Math.random()
 
:

Random rock = new Random();
rock.setSeed(System.nanoTime());
double test = rock.nextDouble();


Yes, I have been playing with setSeed but nothing seems to change the end result. I now believe I have made the mistake in my loops and it has nothing to do with the RNG. It is the only logical conclusion after having tried all the different RNG techniques.


All times are GMT -5. The time now is 4:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC