|
-
September 1st, 2010, 07:00 AM
#1
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
-
September 1st, 2010, 07:03 AM
#2
Re: Generating random numbers
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.
Norm
-
September 1st, 2010, 11:10 AM
#3
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.
-
September 1st, 2010, 11:20 AM
#4
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
Norm
-
September 1st, 2010, 11:47 AM
#5
Re: Generating random numbers
 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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 1st, 2010, 11:53 AM
#6
Re: Generating random numbers
 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... 
I'm glad I'm out of it - retired a month ago! 
Plan to throw one away; you will anyhow...
F. Brooks
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 1st, 2010, 12:22 PM
#7
Re: Generating random numbers
I'm glad I'm out of it - retired a month ago!
Lucky you, some of us still have to work for a living
-
September 2nd, 2010, 11:03 AM
#8
Re: Generating random numbers
-
September 2nd, 2010, 11:48 AM
#9
Re: Generating random numbers
 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 
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 3rd, 2010, 01:52 PM
#10
Re: Generating random numbers
Just think, you'll have loads of free time to answer all those "do my homework for me" posts
-
September 5th, 2010, 04:34 PM
#11
Re: Generating random numbers
 Originally Posted by keang
Just think, you'll have loads of free time to answer all those "do my homework for me" posts 
Oh joy 
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.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 4th, 2011, 01:44 PM
#12
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.
-
January 4th, 2011, 01:45 PM
#13
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.
-
January 4th, 2011, 01:59 PM
#14
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|