Results 1 to 3 of 3
  1. #1
    dredd is offline Member
    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Default How can i generate random indices of a matrix cell?

    I defined the matrix of int's to be 10 by 10 and i want random position of a cell to be picked starting from index row=5 and cell=0 to row=10 and cell=10 and i want to pick 7 times a cell(a different cell each time) and put in the cell the value of 1(other cells will be zeros).

    How can it be done?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: How can i generate random indices of a matrix cell?

    I think it would be easier if you define a "row range" and a "cell range", and that would define a "block" of cells that fall within that given zone.

    Then, you could generate 2 random numbers, R and C, that are locked into the "row range" and "cell range", respectively.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: How can i generate random indices of a matrix cell?

    Define a class Point (x,y) or use an existing class and stick all points that can possibly be selected in a Collection like a List, call Collections.shuffle(yourCollection) and finally use the first seven Points from your Collection. Alternatively select a random point and check if it is already selected before, if not, definitely select it and stick the Point in the 'taboo' List or Set or whatever.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. to generate random lottery no
    By fakepics500 in forum New To Java
    Replies: 19
    Last Post: 07-08-2011, 10:14 AM
  2. [Q] Generate Random Letter
    By iriscience in forum New To Java
    Replies: 11
    Last Post: 01-31-2011, 01:10 AM
  3. Random integer generate
    By trbLeeciN in forum New To Java
    Replies: 6
    Last Post: 06-22-2010, 02:19 AM
  4. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  5. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •