Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random #s? 1

Status
Not open for further replies.

jaredn

Programmer
Sep 1, 1999
1,506
US
Let me start off by saying Im new to this whole Java thing.
All right, heres my problem:

public String encryptIt(String txt,int key)
{
int len = txt.length();int dx;String xi;
String numstr = "";Random r2=new Random();
char chararray[] = txt.toCharArray();
for(int i=0;i<len;i++)
{
int beef = (int)(r2.nextDouble()*10+1);
xi=giveLets(beef);
dx=(int)chararray;
dx=dx<<key;
dx=dx^key;
numstr+=dx+xi;
}
return numstr;
}

this method works fine sending a different random number to beef each time...the problem however lies in the method called giveLets():

private String giveLets(int many)
{
String d=&quot;&quot;;int x;char p;Random r1 = new Random();
for(int i=0;i<many;i++)
{
x=(int)(r1.nextDouble()*126+34);
x=(x>=48&&x<=59)?x+20:x;x=(x>128)?x-35:x;
p=(char)x;d+=p;
}
return d;
}

the first (some random number of :) times I call it, the letters it returns are the same... after that it ends up changing the string...please help me someone!!

also are there any regular expressions in Java? and does anyone know a good online reference?

jared@aauser.com
 
As to your first question, it looks like a nitpicky coding bug, but I can't tell without spending a lot of time trying to run it myself. As to the other question, there isn't a regular expressions package in the standard specification that I know of but other people have created packages (though I have no idea how good they are).
Here's one I saw the other day.


--Will Duty
wduty@radicalfringe.com
 
The Random class always gives the same result after being instantiated. Once you call nextDouble() it starts getting unique. I think that you may have to call this once after instantiation to start getting really random.....

might be...
[tt]Random r2=new Random();r2.nextDouble();[/tt]

...always got time to nitpick ;-) My Home -->
 
thanks for your help... I am calling nextDouble() in there.. Am I using incorrectly? jared@aauser.com
 
I have a feeling that the first time you call nextDouble() it is always giving the same result. If you call it a second time it is then (pseudo)random. What I am suggesting is doing a dummy call to nextDouble() so that your call to set [t]beef[/i] will actually be your second call.

Try putting some System.out.println's to examine the values being produced.... My Home -->
 
The java.util.Random class has a second constructor which takes a (long) seed value. Use some thing like the current time value and this will produce different starting points and therefore different patterns of numbers.

java.util.Date d = new java.util.Date();
Random r = new Random( d.getTime());

Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top