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

    Generate a random number between 1 and 4?

    I need to know how to generate a random number between 1 and 4, I have googled it but i seem to only be able to find results which have really long winded amounts of code, and im thinking theres bound to be a nice short way... any suggestions?

    Thanks in advance

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Generate a random number between 1 and 4?

    Try googling again

    If you can't figure out how to get a random number between 1 and 4 from the very first link then you need to read a Programming 101 book. Don't be lazy, sit and actually read something and "try" to learn instead of having the answer handed to you.

  3. #3
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Generate a random number between 1 and 4?

    PHP Code:
    public String generateRandomNumber(){
       
    System.out.println("3"); //I guarantee that this was a random number

    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  4. #4
    Join Date
    Sep 2008
    Posts
    49

    Re: Generate a random number between 1 and 4?

    I must admit, the replies are very useful...

    the term forum, indicates that this is a place where people can come to exchange ideas, or help one another, a social place to talk about given topics... not to just take the piss...

    if id have been looking for a bunch of stuck up people to reply and try to make themselves look ever so intelligent just because they have had more experience, then i would have tried the sun java forums... wasnt expecting that kind of behaviour on this board.

  5. #5
    Join Date
    Dec 2008
    Posts
    17

    Re: Generate a random number between 1 and 4?

    Ya every forum has it's jagoffs, just can't live without em.

    All that you really need to do is import the java package for randomizing

    import java.util.random;

    Then assign a variable as such:

    Random var = new Random();

    Assign another variable to represent the randomly generated number

    int num = v.nextInt(4) + 1;

    You need to use the + 1 portion because the randomizer will generate 4 random numbers between 0 and 3. Adding one increases the field to 1 and 4

  6. #6
    Join Date
    Oct 2008
    Posts
    77

    Re: Generate a random number between 1 and 4?

    Quote Originally Posted by jenseits View Post
    Ya every forum has it's jagoffs, just can't live without em.

    All that you really need to do is import the java package for randomizing

    import java.util.random;

    Then assign a variable as such:

    Random var = new Random();

    Assign another variable to represent the randomly generated number

    int num = v.nextInt(4) + 1;

    You need to use the + 1 portion because the randomizer will generate 4 random numbers between 0 and 3. Adding one increases the field to 1 and 4
    not quite right, your range is [1,5], not [1,4]
    what he needs is
    Code:
    int num =var.nextInt(3)+1
    this will also ensure even distribution between 1 and 4.

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

    Re: Generate a random number between 1 and 4?

    Gotta say I agree with ProgramThis - the very first link in the Google search he kindly supplied had examples of what the OP was looking for.

    This is a forum for helping people with Java problems they are struggling with, not a Google proxy. Not being able or bothered to use Google doesn't rate anywhere on my list of tricky Java problems.

    The generation of random numbers is too important to be left to chance...
    R. Coveyou
    Last edited by dlorde; April 17th, 2009 at 06:45 AM.
    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.

  8. #8
    Join Date
    Feb 2008
    Posts
    966

    Re: Generate a random number between 1 and 4?

    Quote Originally Posted by Slippy View Post
    the term forum, indicates that this is a place where people can come to exchange ideas, or help one another, a social place to talk about given topics... not to just take the piss...
    Sure, exchanging ideas is great. Sharing information is great. What did you bring to the table? You asked a question to a very simple problem that could have been discovered by yourself with 5 minutes of effort. To me, that is lazy (possibly inept).

    Quote Originally Posted by Slippy View Post
    if id have been looking for a bunch of stuck up people to reply and try to make themselves look ever so intelligent just because they have had more experience, then i would have tried the sun java forums... wasnt expecting that kind of behaviour on this board.
    Stating that you have searched Google and stating that all the results had "long winded amounts of code" is absurd. I didn't answer the way I did to make myself seem smarter. I did it because your post showed a lack of initiative and effort. We get about 5 posts a day here from lazy programmers asking for the answer instead of asking for help.

    Quote Originally Posted by jenseits View Post
    Ya every forum has it's jagoffs, just can't live without em.

    All that you really need to do is import the java package for randomizing

    import java.util.random;

    Then assign a variable as such:

    Random var = new Random();

    Assign another variable to represent the randomly generated number

    int num = v.nextInt(4) + 1;

    You need to use the + 1 portion because the randomizer will generate 4 random numbers between 0 and 3. Adding one increases the field to 1 and 4
    Now this is a guy who is posting to try and show off how smart he is. Instead of leading the OP to find the answer for himself, possibly *gasp* learning something along the way, you decide to just hand out the answer. That to me shows the real "jagoff"

  9. #9
    Join Date
    Dec 2008
    Posts
    17

    Re: Generate a random number between 1 and 4?

    Quote Originally Posted by ProgramThis View Post
    Sure, exchanging ideas is great. Sharing information is great. What did you bring to the table? You asked a question to a very simple problem that could have been discovered by yourself with 5 minutes of effort. To me, that is lazy (possibly inept).



    Stating that you have searched Google and stating that all the results had "long winded amounts of code" is absurd. I didn't answer the way I did to make myself seem smarter. I did it because your post showed a lack of initiative and effort. We get about 5 posts a day here from lazy programmers asking for the answer instead of asking for help.


    Now this is a guy who is posting to try and show off how smart he is. Instead of leading the OP to find the answer for himself, possibly *gasp* learning something along the way, you decide to just hand out the answer. That to me shows the real "jagoff"
    Right because i couldn't possibly be trying to help the guy out. He asked a question, so i showed him an answer, but now he knows where to look should he get stuck. It's one thing to point out a search and link it, it's another to completely condescend to the guy

  10. #10
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Generate a random number between 1 and 4?

    all sounds great except the fact that we are talking about the difficulties of typing "java generate random number" in google and copy-pasting the **** code...
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

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

    Re: Generate a random number between 1 and 4?

    Quote Originally Posted by jenseits View Post
    Right because i couldn't possibly be trying to help the guy out. He asked a question, so i showed him an answer, but now he knows where to look should he get stuck.
    You don't do people's homework for them as well do you? - I only ask because we also get a lot of 'do my homework' questions we feel unable to answer, and having a Google-proxy-cum-homework-writer on board would save us some time

    Seriously though, I think it's reasonable to expect minimal initiative and effort (i.e. enough to Google the answer) before posting on a forum.

    Men are made stronger on realization that the helping hand they need is at the end of their own arm...
    Sidney J. Phillips
    Last edited by dlorde; April 17th, 2009 at 01:47 PM.
    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.

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