Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:31+00:00 2026-05-27T07:20:31+00:00

im trying to implement a hash function in python. Would you consider the following

  • 0

im trying to implement a hash function in python. Would you consider the following a real hash function? I’ve 10 buckets and values from 1 to 7. It will also count amount of collisions 🙂

import random

A=[1,2,3,4,5,6,7]
hashed=[]

def func():
     i=0
     count=0
     while len(A)>i:
          m=random.randint(1,10) # 10 buckets
          if m in hashed:
             count+=1
          hashed.append(m)
          print "element:",A[i], "hashed to bucket", m
          i+=1


     print "Amount of collisions:", count  


func()

Test:

element: 1 hashed to bucket 3
element: 2 hashed to bucket 2
element: 3 hashed to bucket 10
element: 4 hashed to bucket 8
element: 5 hashed to bucket 3
element: 6 hashed to bucket 10
element: 7 hashed to bucket 4
Amount of collisions: 2

EDIT:

I looked at all the comments and tried to create another hash function. This time I use random to determine the keys that are to be hashed. This time i only have 3 buckets. I will try with 25 values that are between 1 and 10:

import random


count=[]

list1 = []  # bucket 1
list2 = []  # bucket 2
list3 = []   # bucket 3

the_list = []
the_list.append(list1)
the_list.append(list2)
the_list.append(list3) # using lists within a list


def func():
   while True:
       number=random.randint(1,10)
       i=random.randint(0,len(the_list)-1)
       the_list[i].append(number)
       count.append(number)
       if len(count)>25: # testing for 25 values
           break

func()
print "Bucket 1:", the_list[0]
print "Bucket 2:", the_list[1]
print "Bucket 3:", the_list[2]

Test:

Bucket 1: [5, 9, 8, 10, 3, 10]
Bucket 2: [10, 5, 8, 5, 6, 2, 6, 1, 8]
Bucket 3: [9, 4, 7, 2, 1, 6, 7, 10, 9, 1, 5]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:20:32+00:00Added an answer on May 27, 2026 at 7:20 am

    No. A hash function has to be deterministic. It cannot rely on randomness.

    A hash procedure must be deterministic—meaning that for a given input value it must always generate the same hash value. In other words, it must be a function of the hashed data, in the mathematical sense of the term. This requirement excludes hash functions that depend on external variable parameters, such as pseudo-random number generators or the time of day. It also excludes functions that depend on the memory address of the object being hashed, because that address may change during execution (as may happen on systems that use certain methods of garbage collection), although sometimes rehashing of the item is possible).

    Source: Hash function – Determinism (Wikipedia)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I
I'm trying to implement a function which returns a string of hex values. I
Am trying to implement a generic way for reading sections from a config file.
I am trying to implement string unescaping with Python regex and backreferences, and it
I'm trying to implement some data structures (like HAMP-hash array mapped trie) in Common
I'm trying to implement a Chord distributed hash table. I want to use SHA-1
I'm trying to implement this protocol: http://en.wikipedia.org/wiki/Chord_(peer-to-peer ) What i understood from it is
I'm trying to implement an efficient hash table where collisions are solved using linear
I am trying to implement a hash table
Possible Duplicate: The fundamentals of Hash tables? I am trying to implement a Simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.