• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To generate random integer numbers between specific range

 
Greenhorn
Posts: 4
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to write a code to generate set of arrays in which each array should contain distinct five integers arranged in ascending order and range in which random number can be generated is between 1-52 both inclusive.....but i have written code below for just generating five random integers and it is not working first two numbers are always 0....can any body tell me whats the heck!!??

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Random number generators in nearly all languages default to using the same seed, which makes then produce the same sequence of results. If you want to get different numbers each time you run, use something that varies, like the current time, as an argument to set the seed. Its usually easier to debug your code when you have the same sequence over and over -- once its working, you then set a non-default seed.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags, which you should always use, to your post, and you can see how much better it looks I also removed some blank lines, because they don't always make it easier to read.

I ran your code, and removed lines 2, 7, 8 and 9, which don't do anything. I think the problem has nothing to do with the randomness of the numbers. You will occasionally get 0 0 0 .... printed out.

java CollTest2
0
0
7
22
30
java CollTest2
0
0
7
31
47
java CollTest2
0
0
14
35
43
etc...

I suggest you go through the flow of control like this, on a sheet of paper.
  • first random number is 28
  • array is now[28,0,0,0,0]
  • The next thing I write down will explain why the first number is always 0
  • As I said, follow the flow control carefully.
     
    Mudit Agarwal
    Greenhorn
    Posts: 4
    Eclipse IDE Postgres Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hey Campbell thanks for posting on my query will definitely go through flow again..and will post the problem if it occurs again

    yea actually i was writing code for something else ..code i posted is a very small part of that program,complete question i am mentioning below

    Question -1- write a program (any language you want) to loop through all possible poker hands once each. Here, a “poker hand” is a list of 5 distinct numbers from 1..52, e.g. {2, 3, 7, 13, 40}. Your program should be able to generate each such array, with
    the 5 numbers in ascending order.....
     
    Bartender
    Posts: 1166
    17
    Netbeans IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mudit Agarwal wrote:hey Campbell thanks for posting on my query will definitely go through flow again..and will post the problem if it occurs again

    yea actually i was writing code for something else ..code i posted is a very small part of that program,complete question i am mentioning below

    Question -1- write a program (any language you want) to loop through all possible poker hands once each. Here, a “poker hand” is a list of 5 distinct numbers from 1..52, e.g. {2, 3, 7, 13, 40}. Your program should be able to generate each such array, with
    the 5 numbers in ascending order.....



    This does not require any random numbers; it requires that you pick all combination of 5 from 52 using an ordered approach. This can be done either by using 5 'for' loops or by recursion to a depth of 5 and no actual sorting is required since both approaches automatically sort the output.
     
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Mudit,

    hope you figured out why you always get two zeroes.

    If I look at your original listing, I wonder: how are you going to make sure you have 5 distinct values?
    And a randomizer will not help you to list all possible hands (which are over 300M).

    Greetings,
    Piet
     
    Mudit Agarwal
    Greenhorn
    Posts: 4
    Eclipse IDE Postgres Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey Piet ,
    yes i figured out why i was getting two zeroes at the starting it got resolved.

    I think my approach to the problem is not correct..and yes randomizer is not always giving 5 distinct number and also i was thinking how i will loop through all the sets of possible poker hands.... is it possible can you help with the algorithm of the problem.

    Regards

    Mudit
     
    Richard Tookey
    Bartender
    Posts: 1166
    17
    Netbeans IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mudit Agarwal wrote:. is it possible can you help with the algorithm of the problem.t



    It is almost trivial ! Run a loop with an index (say a) from 1 to (52-4). Inside that run a loop with an index (say b) from ((a+1) to (52-3). Inside that run a loop with an index (say c) ...

    Get the idea?

    Inside the inner most loop the loop variables are the values you want !


     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Mudit,

    What Richard writes is what I would have written. What I always do with questions like these, is to picture a very
    simplified verios, write it out and see if I see some patterns arising.

    So, lets take a sorted set of 3 out of a collection of 5.

    Now, if you write out all possibilities, in an orderly way, you get the following possible outcomes:

    {1, 2, 3}

    (1, 2, 4}

    (1, 2, 5}

    {1,3, 4}

    {1, 3, 5}

    {1, 4, 5}

    {2, 3, 4}

    {2, 3, 5}

    {2, 4, 5}

    {3, 4, 5}

    Ten in all. Do you see a pattern here? It is exacly what Richard described.

    Now, the general formula for a sorted set of K diifrent elements out of a set of N different elements, is

    N * (N-1) * ... * (N - K + 1) / [ 1 * 2 * ... * K ] usually written as

    ( N )
    ( K )

    In the example from 3 out of 5, you get 5 * 4 * 3 / (1 * 2 * 3) = 10

    and for the poker hands: 5 out of 52, or ...

    Indeed, it is almost trivial (but only if you are almost a mathematician!)

    Greetings,
    Piet
     
    Mudit Agarwal
    Greenhorn
    Posts: 4
    Eclipse IDE Postgres Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Piet and Richard for nice explanation to the problem i got my code working...now i have to make few amendments in my base code to solve more quetions related to the base code. Here i am posting my code and the questions that i am trying to solve based on it.


    1.Print each poker hand on its own line, and count the number of unique lines you have....(This also helps verifying your code)
    2. Check if there are any lines that are duplicates
    3. Count the number of poker hands whose numbers add up to 130.
    3. Count the number of poker hands whose numbers add up to a multiple of 16.
    4. Count the number of poker hands whose numbers multiply together to form a product that is a multiple of 45 but not a multiple of 27.
    5. Instead of simply looping through every poker hand, you should be able to input any number between 1 and total number of lines, and your program should output a unique poker hand associated with that number.
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Mudit,

    now, questions 1 and 2 are simple to answer. The method used to generate all the hands garantee the uniqueness of each hand.
    The 'count' variable is the total number. You have that already,

    Also, you have the number of hands for which the sum of the values equals 130.
    In the same fashion you can check if that value is a multiple of 16, and also if the product
    is a multiple of 45 but not a multiple of 27.

    Nastier is the last question. The way you generate the hands now makes that you loose
    the hand after each iteration. Also, if you look at the code,you will probably admit that
    it looks a bit messy.

    To tackle both problems, I would suggest to define a 'Hand' class, something simple like
    and instead of your int[]A, you simply create a new Hand.

    Now, I don't know if you are allowed to use an ArrayList, but if so,
    create an ArrayList<Hand>, and add each Hand to that list.

    If not, I suggest you create an array of about 3.000.000 entries,
    like Hand[] hands = new Hand[3000000],
    and when you have created a new Hand, you add it to the array by
    hand[count] = Hand_just_created.

    Having produced a complete array of Hands, you can do, glass of bear
    next to the pc, big sigar in the mouth, huge grin on the face, perform the questions, like:

    and (in pseudo code)

    Greetz,
    Piet

    Edit: by the way, almost forgot to tell: did you reset the variable 'eq' to 0, in ech iteration?

    Edit 2: added a 'toString' method to the Hand class
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey! I was doing this task with modern java and I was able to code a very simple function generateRandomInteger following this tutorial to generate random integers with java 1.7 or later.

    Hope this information helps to anyone. If you don't want to follow the link and just use the function:

     
    Saloon Keeper
    Posts: 10705
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Pat Farrell wrote:Random number generators in nearly all languages default to using the same seed, which makes then produce the same sequence of results. If you want to get different numbers each time you run, use something that varies, like the current time, as an argument to set the seed. Its usually easier to debug your code when you have the same sequence over and over -- once its working, you then set a non-default seed.


    From the documentation for Random:

    public Random()
    Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

    So, using this constructor will start with a random seed, not a default seed. If you want to control the seed being used you need to use the Random(seed) constructor.
     
    Carey Brown
    Saloon Keeper
    Posts: 10705
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    nithatufro tufro wrote:Hey! I was doing this task with modern java and I was able to code a very simple function generateRandomInteger following this tutorial to generate random integers with java 1.7 or later.

    Hope this information helps to anyone. If you don't want to follow the link and just use the function:


    See prior post. Using currentTimeMillis() is not as random as what you'd get using the empty constructor, Random().
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    nithatufro tufro wrote:. . . this tutorial. . . .

    That tutorial is not very helpful; as well as what Carey says, it doesn't explain the potential problems with the different techniques, nor give you a recommendation about which is best. It also for some peculiar reason uses thread local random rather than java.util‍.Random. Yoiu will find a thorough discussion here, but of course all those discussions are out of date now the Random#ints() method and simlar methods are available.

    And welcome to the Ranch
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic