Solved

# making random numbers within a range

Posted on 2004-11-13
Medium Priority
236 Views
I am writinging a test program involving a hash table. I am suposed touse a for-loop to insert int 15 keys with random double values.

here's my insert method:

String s = "abcdefghijklmopqrstuvwxyz";
public void insert(int key, double data)
{
probes = 0;
if(fullTable())
{
System.out.println("table is full, cannot insert");
}
hashNode h = new hashNode(key, data);
int index = hash(key);
int attempt = 0;
for(int i = 0; i < size; i++)
{
if(space[index] == null)
{
space[index] = h;
attempt++;
probes++;
System.out.println("probed " + probes + " times to insert " + s.charAt(key - 65));
break;
}
else
{
if(space[index].getKey() == key)
{
space[index].setData(data);
break;
}
index = rehash(key, attempt);
attempt++;
probes++;
System.out.println("probed " + probes + " times to insert " + s.charAt(key - 65));
}
}
count++;
}
and heres my test so far:
import java.util.*;

public class hashTest2
{
public static void main(String[] args)
{
Random r =new Random();
myHash2 m = new myHash2(26);

for(int i = 0; i < 15; i++)
{
m.insert(r.nextInt(25), r.nextDouble(20.0));    //note
}
}
}

where it says note: it gives me an error that says r.nextDouble cant be applie to (double)
also in the insert, I am supposed to convert the key int  to a char. I am using a String of the alphabet and using charAt(<int value>) I am stuck
Thank you
DAJones
0
Question by:DAJones
• 4
• 3

LVL 92

Expert Comment

ID: 12575997
nextDouble() doesn't atke an arg and returns a value between 0 and 1.0

r.nextDouble()*20.0
0

LVL 92

Accepted Solution

objects earned 500 total points
ID: 12576001
and you need to wrap the primitives to store in map:

m.insert(new Integer(r.nextInt(25)), new Double(r.nextDouble()*20.0));
0

Author Comment

ID: 12576019
OK, now Im getting a stringIndexOutOfBounds error this has to be in my insert

0

LVL 92

Expert Comment

ID: 12576034
actually ignore my comment about wrapping the primitive,I didn't notice you were using your own map implementation
0

LVL 92

Expert Comment

ID: 12576050
check your hash function returns a value within the bounds of your array.
0

Author Comment

ID: 12576256
ok, heres what I'm doing im converting ints to chars. a = 65, b = 66, ... , z = 90. my code is working, but sometimes I'm getting values < 65, abd this is when I am getting the string out of bounds. but I thought my   System.out.println("probed " + probes + " times to insert " + s.charAt(key - 65)); lin would take care of this.
0

LVL 30

Expert Comment

ID: 12577213
What happens if you use r.nextDouble () and multiply it with 65 (and ceil it)?
0

Author Comment

ID: 12578422
The data variable just needs to be a random double. I have no other restictions on this. I did solve my out of bounds problem by canging this line in my insert: from
System.out.println("probed " + probes + " times to insert " + s.charAt(key - 65));  to
System.out.println("probed " + probes + " times to insert " + s.charAt(key));
and in the test this line:
m.insert(r.nextInt(90), r.nextDouble(20.0));
to
int x = r.nextInt(25);
double  y = r.nextDouble * 20;
m.insert(x, y);

it turns out I was combining two of my problems, on this one I dont need to convert! ha
Dajones
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month17 days, 6 hours left to enroll