MT or KISS ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

kbhearn
Posts: 411
Joined: Thu Dec 30, 2010 4:48 am

Re: MT or KISS ?

Post by kbhearn »

your zobrist hash is already effectively a random value distributing things over all buckets. you can use modulus by number of buckets (does not need to be any special number), but you can also make your number of buckets 2^n. thus the modulus is the same as just the lowest n bits and you can get your bucket number with a simple bit-wise and.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: MT or KISS ?

Post by lucasart »

Dan Honeycutt wrote:
lucasart wrote:
Dan Honeycutt wrote:I found a FORTRAN (which, like Java that I'm working in, uses signed integers)
You seem to be quite a competent programmer. So why do you punish yourself with Java ;)
I can't answer right now, got an appointment with my dominatrix in a little while.

Best
Dan H.
So it's masochism, right ? That's what I thought :D
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: MT or KISS ?

Post by lucasart »

voyagerOne wrote: Also... can I simply use modulus for the hash function? If so... what's a "good" modulus number to use? Should it be prime?
have a look there, there's all you need to know about LCG
http://en.wikipedia.org/wiki/Linear_con ... _generator
petero2
Posts: 690
Joined: Mon Apr 19, 2010 7:07 pm
Location: Sweden
Full name: Peter Osterlund

Re: MT or KISS ?

Post by petero2 »

voyagerOne wrote:@ Peter:

What's the danger of simply using SecureRandom nextLong()?
I don't think SecureRandom is guaranteed to produce the same sequence for all java implementations, even if you seed it with the same value. This is a problem if you want the search to be identical across different platforms and java implementations.
Mincho Georgiev
Posts: 454
Joined: Sat Apr 04, 2009 6:44 pm
Location: Bulgaria

Re: MT or KISS ?

Post by Mincho Georgiev »

This is the full code of mine:

Code: Select all

#include <stdio.h>

#ifndef true
#define true 1
#define false 0
#define bool int
#endif

typedef unsigned long long uint64;
typedef unsigned int uint32;

#define HDMIN 21//the required minimum hamming distance
#define HDMAX 40//the maximum hamming distance
#define HS 4096 //the history stack size
#define MS 10 //the count of separated groups of numbers
//int max_size&#91;MS&#93; = &#123;1920, 16, 128, 2, 0, 0, 0, 0, 0, 0&#125;;//the divided sizes of the groups
int max_size&#91;MS&#93; = &#123;1792, 15, 127, 1, 0, 0, 0, 0, 0, 0&#125;;


uint64 __inline rand64&#40;)
&#123;//generates 64-bit rnd number
    return (&#40;uint64&#41;rand&#40;) 
	 ^ (&#40;uint64&#41;rand&#40;) << 15&#41; 
	 ^ (&#40;uint64&#41;rand&#40;) << 30&#41; 
	 ^ (&#40;uint64&#41;rand&#40;) << 45&#41; 
	 ^ (&#40;uint64&#41;rand&#40;) << 60&#41;);
&#125;

bool __inline long_enough&#40;uint64 rn&#41;
&#123;//checks if the number has a 16 digits and not less
	int x;
	char strbuff&#91;24&#93; = &#123;0&#125;;
	sprintf&#40;&strbuff&#91;0&#93;, "%#llx", rn&#41;;
  x = strlen&#40;strbuff&#41;;
  if &#40;x < 18&#41; return false;
  return true;
&#125;

uint32 __inline hamdist&#40;uint64 x, uint64 y&#41;
&#123;//returning the hamming distance between the 2 bitstrings&#58;
	uint32 dist = 0;
	uint64 val = x ^ y;
  while&#40;val&#41;
  &#123; ++dist;
    val &= val - 1;
  &#125;
  return dist;
&#125;

int main&#40;)
&#123;
	int i,j,k; //loop counters;
  FILE *f; //the 'numbers.txt' file descriptor
  int tab = 0; //'4 on a line' counter
  uint64 rnd_number; //the current rnd number
  uint64 history_stack&#91;HS&#93; = &#123;0&#125;;//stack of generated numbers, the 1s element is independent - 'to compare with'
  int hs_pos = 1; //current position in the history stack
  bool num_ok = false;
  int hd = 0;//hamming distance returned value
  uint32 num_count = 0;
  uint32 total_num_count = 0;
	
  //calculating the total
  for&#40;i = 0; i < MS; i++)
    total_num_count += max_size&#91;i&#93;;
  printf&#40;"total&#58; %d\n",total_num_count&#41;;
  printf&#40;"current&#58;\n");

  //setting the first element &#40;separately from the rest&#41;
  do&#123;rnd_number = rand64&#40;);	
  &#125;while&#40;!long_enough&#40;rnd_number&#41;);
  history_stack&#91;0&#93; = rnd_number;
		
  //start&#58;
  f = fopen&#40;"numbers.txt", "w");
  for&#40;i = 0; i < MS; i++)
  &#123;
    for&#40;j = 0; j < max_size&#91;i&#93;;j++)
    &#123; do
      &#123; num_ok = true;
        //checking the lenght after generate&#58;
        do&#123;rnd_number = rand64&#40;);	
        &#125;while&#40;!long_enough&#40;rnd_number&#41;);
        //checking the hd with the rest numbers in stack&#58;
        for&#40;k = 0; k < hs_pos; k++)&#123;
        //check if the number exists &#40;duplicate&#41;
          if&#40;rnd_number == history_stack&#91;k&#93;)
          &#123; num_ok = false;
            break;
          &#125;
          hd = hamdist&#40;rnd_number, history_stack&#91;k&#93;);
          if&#40;&#40;hd < HDMIN&#41; || &#40;hd > HDMAX&#41;)
          &#123; num_ok = false;
            break;
          &#125;
        &#125;
      &#125;while&#40;!num_ok&#41;;
      
      //the number is ok now, so increase the position in stack and record it&#58;
      history_stack&#91;++hs_pos&#93; = rnd_number;
      //saving&#58;
      tab++;
      fprintf&#40;f, "\t%#llx", rnd_number&#41;;
      fprintf&#40;f, "%s", "ULL");
      if&#40;&#40;j + 1&#41; != max_size&#91;i&#93;) fprintf&#40;f,"%s",",");
      //the line of 4 is ended ?&#58;
      if&#40;tab == 4&#41; &#123;
        fprintf&#40;f, "%s", "\n");
        tab = 0;
      &#125;
      //printing out the recorded count so far&#58;
      num_count++;
      printf&#40;"\r%d",num_count&#41;;
    &#125;
    //separating the portions in file&#58;
    fprintf&#40;f, "%s", "\n\n\n\n");
  &#125;//exit&#58;
  fclose&#40;f&#41;;
  printf&#40;"\n\n");
  system&#40;"pause");
  return 0;
&#125;

AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: MT or KISS ?

Post by AlvaroBegue »

Chances are any method is good enough for your purpose. But if you want to get really good random numbers and you are using Linux, read the numbers you need from /dev/random and build a hard-coded table with them. It might take a while of moving the mouse around and typing garbage for it to gather enough entropy, but the results are likely to be very good random numbers.