2018-11-09 Random

posted Nov 9, 2018, 6:23 AM by Konstantinovich Samuel   [ updated Nov 13, 2018, 6:27 PM ]

Now let us look at Random:

import java.util.Random;
public class DemoRandom{
  public static void main(String[]args){

    //here no seed is provided, so it uses the clock as a seed
    //this can be used to produce a random seed for a different Random object!
    Random rng = new Random();
    for(int i =  0; i < 5; i++ ){
      System.out.println(rng.nextInt() % 1000);

    //specified seed can be used to reproduce sequences
    rng = new Random(100);//seed of 100 is stored.
    for(int i =  0; i < 5; i++ ){
      System.out.println(rng.nextInt() % 1000);

WordSearch updates

1. Make a plain text file "words.txt" and fill it with whatever words you like. 
   -Put in words with different lengths to help you test
Your text file should be formatted like this:

Update to your WordSearch with new instance variables + Methods
(Due Wednesday, DO NOT wait until Tuesday)

import java.util.*; //random, scanner, arraylist
import java.io.*; //file, filenotfoundexception
public class WordSearch{
    private char[][]data;

    //the random seed used to produce this WordSearch
    private int seed;

    //a random Object to unify your random calls
    private Random randgen;

    //all words from a text file get added to wordsToAdd, indicating that they have not yet been added
    private ArrayList<String>wordsToAdd;
    //all words that were successfully added get moved into wordsAdded.
    private ArrayList<String>wordsAdded;


Update your wordSearch with the methods discussed in class:

Two Constructors:
    Both will read in the word text file, then run addAllWords(). Do not fill in random letters after.

public WordSearch( int rows, int cols, String fileName) 
    Choose a randSeed using the clock random

public WordSearch( int rows, int cols, String fileName, int randSeed)
    Use the random seed specified.

Three Methods

1. public String toString()

toString should print in the following format: 
  • Use '|' as left/right boundaries of the grid. 
  • One long line of comma separated words after the string "Words: "
  • When you got all other things working, add the seed 

|_ _ F _|
|_ A I _|
|D _ S _|
|M E H _|
Words: FAD, FISH, MEH (seed: 103)

When printing the final puzzle, you will add random letters to fill in the _'s

AddWord (Edit: corrected the order of parameters, since it is private, the wrong order will not affect your code)
2. private boolean addWord( String word, int r, int c, int rowIncrement, int colIncrement)


   /**Attempts to add a given word to the specified position of the WordGrid.
     *The word is added in the direction rowIncrement,colIncrement 
     *Words must have a corresponding letter to match any letters that it overlaps.
     *@param word is any text to be added to the word grid.
     *@param row is the vertical locaiton of where you want the word to start.
     *@param col is the horizontal location of where you want the word to start.
     *@param rowIncrement is -1,0, or 1 and represents the displacement of each letter in the row direction
     *@param colIncrement is -1,0, or 1 and represents the displacement of each letter in the col direction
     *@return true when: the word is added successfully.
* false when: the word doesn't fit, OR rowchange and colchange are both 0,
*        OR there are overlapping letters that do not match */ private boolean addWord(String word,int row, int col, int rowIncrement, int colIncrement){ }

    /*[rowIncrement,colIncrement] examples:
     *[-1,1] would add up and the right because (row -1 each time, col + 1 each time)
     *[ 1,0] would add downwards because (row+1), with no col change
     *[ 0,-1] would add towards the left because (col - 1), with no row change

3. private void addAllWords() 
EDIT: There is no returned value! This is not useful since it will add 0 or more words no matter what.
Attempt to add all of the words from the wordsToAdd list using the following algorithm:
  1. Choose a random word, and a random direction (rowIncrement/colIncrement)
  2. Try to add that word to different starting positions* until: 
    • you successfully add the word
    • you run out of positional tries 
  3. Repeat this process until you added all of the words, or you tried to add unsuccessfully too many times in a row. 
  4. OPTIONAL    Optimize your addAllWords such that: 
    • You do not try to add words to positions that would not fit the word on the board. **
    • Use the rowIncrement/colIncrement to decide what the range of valid row/col should be.

    *Each try in step 2 only changes the position of the start, not the direction or word
    **A five letter word that is meant to be added across to the right should not START in the last 4 columns of the board    

Make sure your toString verifies your words are adding properly. Feel free to print the object inside of its own methods as a debug!