/**                                                                                                                                                                                
 * Copyright (c) 2010 Yahoo! Inc. All rights reserved.                                                                                                                             
 *                                                                                                                                                                                 
 * Licensed under the Apache License, Version 2.0 (the "License"); you                                                                                                             
 * may not use this file except in compliance with the License. You                                                                                                                
 * may obtain a copy of the License at                                                                                                                                             
 *                                                                                                                                                                                 
 * http://www.apache.org/licenses/LICENSE-2.0                                                                                                                                      
 *                                                                                                                                                                                 
 * Unless required by applicable law or agreed to in writing, software                                                                                                             
 * distributed under the License is distributed on an "AS IS" BASIS,                                                                                                               
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or                                                                                                                 
 * implied. See the License for the specific language governing                                                                                                                    
 * permissions and limitations under the License. See accompanying                                                                                                                 
 * LICENSE file.                                                                                                                                                                   
 */

package com.yahoo.ycsb;

import java.util.Random;

/**
 * Utility functions.
 */
public class Utils
{
	static Random random=new Random();
	
      /**
       * Generate a random ASCII string of a given length.
       */
      public static String ASCIIString(int length)
      {
	 int interval='~'-' '+1;
	 
	 StringBuilder str=new StringBuilder();
	 for (int i=0; i<length; i++)
	 {
	    char c=(char)(random.nextInt(interval)+' ');
	    str.append(c);
	 }
	 
	 return str.toString();
      }
      
      /**
       * Hash an integer value.
       */
      public static int hash(int val)
      {
	 return FNVhash32(val);
      }
	
      public static final int FNV_offset_basis_32=0x811c9dc5;
      public static final int FNV_prime_32=16777619;
      
      /**
       * 32 bit FNV hash. Produces more "random" hashes than (say) String.hashCode().
       * 
       * @param val The value to hash.
       * @return The hash value
       */
      public static int FNVhash32(int val)
      {
	 //from http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
	 int hashval = FNV_offset_basis_32;
	 
	 for (int i=0; i<4; i++)
	 {
	    int octet=val&0x00ff;
	    val=val>>8;
	    
	    hashval = hashval ^ octet;
	    hashval = hashval * FNV_prime_32;
	    //hashval = hashval ^ octet;
	 }
	 return Math.abs(hashval);
      }
      
      public static final long FNV_offset_basis_64=0xCBF29CE484222325L;
      public static final long FNV_prime_64=1099511628211L;
      
      /**
       * 64 bit FNV hash. Produces more "random" hashes than (say) String.hashCode().
       * 
       * @param val The value to hash.
       * @return The hash value
       */
      public static long FNVhash64(long val)
      {
	 //from http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash
	 long hashval = FNV_offset_basis_64;
	 
	 for (int i=0; i<8; i++)
	 {
	    long octet=val&0x00ff;
	    val=val>>8;
	    
	    hashval = hashval ^ octet;
	    hashval = hashval * FNV_prime_64;
	    //hashval = hashval ^ octet;
	 }
	 return Math.abs(hashval);
      }
}

@@ -3007,52 +3007,86 @@
 1;%0A%09
- %0A%09 StringBuilder str=new StringBuilder();%0A%09
+%0A        byte %5B%5Dbuf = new byte%5Blength%5D;%0A        random.nextBytes(buf);%0A       
  for
@@ -3092,22 +3092,26 @@
 r (int i
-=
+ = 
 0; i
-%3C
+ %3C 
 length; 
@@ -3118,47 +3118,78 @@
 i++)
-%0A%09 %7B%0A%09    char c=(char)(random.nextInt(
+ %7B%0A          if (buf%5Bi%5D %3C 0) %7B%0A            buf%5Bi%5D = (byte)((-buf%5Bi%5D %25 
 inte
@@ -3197,51 +3197,129 @@
 val)
-+
+ + 
 ' ');%0A
-%09    str.append(c);%0A%09 %7D%0A%09 %0A%09
+          %7D else %7B%0A            buf%5Bi%5D = (byte)((buf%5Bi%5D %25 interval) + ' ');%0A          %7D%0A        %7D%0A       
  return 
 str.
@@ -3318,21 +3318,22 @@
 urn 
-str.to
+new 
 String(
+buf
 );%0A 

Improve CPU usage of random string generation