Generating random numbers
Good day people,
am struggling with a problem and i hope you can be of help.i want to generate 15 random integer numbers between 0-75,and none of the numbers once generated is repeated,e.g once 2,5,or 11 are generated by the subroutine or function they are not repeated again instead ,math.random() is called again to generate new integers until new integers different from those generated previously are produced...hw do i do this,do i get a variable to save the generated numbers and check this value against a new generated number.
i am developing this application for j2ME so i cant work with vectors or arrays.thanks
Re: Generating random numbers
Quote:
get a variable to save the generated numbers and check this value against a new generated number
Yes.
Wrap the Random method calls in your own method and save each generated number that is returned.
Before returning a number, check if it has been returned previously and if it has, get another one.
Re: Generating random numbers
I don't know much about the ME API, and I absoultely HATE what Oracle has done with java.sun.com :( Jerks.
From what I did find though, it does look like there are HashTables and Vectors available to you. You could always use the HaslTable and store the Integer values as the key. It would simplify you having to iterate through the list every time looking for one.
Re: Generating random numbers
If there are a max of 76 unique numbers to test for, use the bits of two longs to remember what values have been returned. Set a bit with OR and test a bit with AND
Re: Generating random numbers
Quote:
Originally Posted by
Nedman1
i am developing this application for j2ME so i cant work with vectors or arrays.thanks
J2ME supports both Vectors and arrays. One simple solution is to fill a Vector with numbers 1-75 and randomly select an entry (from 0 to size()-1) then remove it. Repeat until you have the number of randoms you need.
No matter how far down the wrong road you have gone, turn back now...
Turkish proverb
Re: Generating random numbers
Quote:
Originally Posted by
ProgramThis
I don't know much about the ME API, and I absoultely HATE what Oracle has done with java.sun.com :( Jerks.
I agree - I thought it was bad under Sun, but Oracle made it an order of magnitude worse - I can't find anything... :sick:
I'm glad I'm out of it - retired a month ago! :D :thumb:
Plan to throw one away; you will anyhow...
F. Brooks
Re: Generating random numbers
Quote:
I'm glad I'm out of it - retired a month ago!
:thumb: Lucky you, some of us still have to work for a living :sick:
Re: Generating random numbers
Quote:
Originally Posted by
keang
:thumb: Lucky you, some of us still have to work for a living :sick:
Yeah, no kidding ;) Congrats though! So, permanent retirement? Golf and cricket and all of that good stuff now?
Re: Generating random numbers
Quote:
Originally Posted by
ProgramThis
Yeah, no kidding ;) Congrats though! So, permanent retirement? Golf and cricket and all of that good stuff now?
Yup, permanent retirement. Watching golf & cricket & all that good stuff - lets not get too energetic :D
I hadn't planned to go quite this early (55) but company pension changes forced my hand. Luckily I have a fairly low cost lifestyle - although I may have a stab at Android app development if I get bored ;)
The generation of random numbers is too important to be left to chance...
R. Coveyou
Re: Generating random numbers
Just think, you'll have loads of free time to answer all those "do my homework for me" posts :p
Re: Generating random numbers
Quote:
Originally Posted by
keang
Just think, you'll have loads of free time to answer all those "do my homework for me" posts :p
Oh joy :rolleyes:
We think too much about effective methods of teaching and not enough about effective methods of learning. No matter how good teaching may be, each student must take the responsibility for his own education...
J. Carolus S.J.
Re: Generating random numbers
norm or anybodyelse please could you help me with some sample code so as to make it clearer to me. thanks and and i appreciate it a lot.
Re: Generating random numbers
norm or anybodyelse please could you help me with some sample code of the above topic(generating random unique numbers) so as to make it clearer to me. thanks and and i appreciate it a lot.
Re: Generating random numbers
Have you accepted that you can use vectors and/or arrays in ME or is there some other reason you can't use them.