Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm working on a java project where I have a method that returns a hash function as a
pair of integers a , b that exist in {1 , 2 , ... , 10000} and are picked randomly . To
pick the random numbers I have an "rnd" variable . My problem is that I have learned that prime numbers are better for hashing a function which means that the number b must be a prime number . With the above information given how can a hash function be randomly
generated ?
I have posted my existing code below .I want to implement the hasfuncgen() function.
Thank you for your time .

What I have tried:

Java
import java.util.Random;

class IntegerPair {
	private int a;
	private int b;
	
	public IntegerPair(int a, int b) {
		this.a = a; 
		this.b = b;
	}	
	
	public int getA() {
		return a;
	}
	public void setA(int a) {
		this.a = a;
	}
	public int getB() {
		return b;
	}
	public void setB(int b) {
		this.b = b;
	}
}

public class Document {
	private int id = -1;
	private int[] tokens = null;
	private String filename = "";
	private double sim = 0.0;
	private static Random rnd = new Random(System.currentTimeMillis());

	public Document(Integer[] tokens, String filename, int id) {
		if (tokens != null) {
			if (this.tokens == null)
				this.tokens = new int[tokens.length];
			for (int i=0 ; i < tokens.length ; i++) {
				this.tokens[i] = tokens[i];
			}
		}
		this.filename = filename;
		this.id = id;
	}

	private IntegerPair hashFuncGen() 
	{		
		return null;
	}
Posted
Updated 28-Dec-19 0:40am
v2

1 solution

You don't use random values to generate a hash - the idea of a hash is that it is a smaller, simpler, easier-to-manipulate version of the original input. And that requires that the same input always gives the same hash value, which use of random numbers does not allow.

Think about it: if a hash is a randomly generated value, what use is it? You can't use it to compare passwords, you can't use it to quickly access an item. So what can you do with it? Nothing that is actually useful ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900