CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2010
    Posts
    3

    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

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  3. #3
    Join Date
    Feb 2008
    Posts
    966

    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.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Generating random numbers

    Quote Originally Posted by Nedman1 View Post
    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.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Generating random numbers

    Quote Originally Posted by ProgramThis View Post
    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.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Feb 2008
    Posts
    966

    Re: Generating random numbers

    Quote Originally Posted by keang View Post
    Lucky you, some of us still have to work for a living
    Yeah, no kidding Congrats though! So, permanent retirement? Golf and cricket and all of that good stuff now?

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Generating random numbers

    Quote Originally Posted by ProgramThis View Post
    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.

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Generating random numbers

    Just think, you'll have loads of free time to answer all those "do my homework for me" posts
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #11
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Generating random numbers

    Quote Originally Posted by keang View Post
    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.

  12. #12
    Join Date
    Sep 2010
    Posts
    3

    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.

  13. #13
    Join Date
    Sep 2010
    Posts
    3

    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.

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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
  •  





Click Here to Expand Forum to Full Width

Featured