Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Random seed a String ?  (Read 2324 times)
0 Members and 1 Guest are viewing this topic.
Offline Cero
« Posted 2012-02-28 16:30:01 »

Looking at the java Random class it supports a long as a seed
minecraft for example has a string seed

without writing it yourself obviously, what would I use if I want a string as a seed ?

edit: or to ask differently: how is this done usually ?
like every character casted to int, and then, added or whatever ?

Offline xsvenson
« Reply #1 - Posted 2012-02-28 16:36:17 »

I think I remember him saying that he just takes the toByteArray() and does something with it. Or even just .hashCode() ?

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline Cero
« Reply #2 - Posted 2012-02-28 17:31:26 »

1  
2  
3  
4  
5  
6  
7  
String strSeed = "yessir";
long seed = 0;
for (int i=0;i<strSeed.length();i++)
{
      seed+=(int)strSeed.charAt(i);
}  
Random random = new Random(seed);

of course this works, but many collisions, as "yessir" and "siryes" will obviously yield the same result (not really sure if thats a problem in my case)


I think I remember him saying that he just takes the toByteArray() and does something with it. Or even just .hashCode() ?
hashCode() huh ?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2012-02-28 18:26:10 »

Yeah: just using the default hashCode result as the seed is good enough for gaming purposes.
Offline Cero
« Reply #4 - Posted 2012-02-28 19:14:44 »

well hashcode is the hashcode of the OBJECT

so it seems  "blah".hashCode() always returns the same thing - if thats true, well then good, although I dont get it
because
1  
2  
"blah".hashCode();
"blah".hashCode();

2 lines of code, 2 strings, 2 different objects, right ? They just happen to have the same string value.

Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-02-28 19:19:03 »

That's because String overrides hashCode() to return the hash of the String.

Offline Cero
« Reply #6 - Posted 2012-02-28 21:29:37 »

That's because String overrides hashCode() to return the hash of the String.

alright then

Offline pjt33
« Reply #7 - Posted 2012-02-29 11:51:38 »

so it seems  "blah".hashCode() always returns the same thing - if thats true, well then good, although I dont get it
because
1  
2  
"blah".hashCode();
"blah".hashCode();

2 lines of code, 2 strings, 2 different objects, right ? They just happen to have the same string value.
Apart from the fact that you actually have one object there due to string interning, if you expect "blah".equals("blah") to return true then either you expect "blah".hashCode() == "blah".hashCode() to return true or you expect HashMap to break badly when strings are used as keys.
Offline Danny02
« Reply #8 - Posted 2012-02-29 13:04:50 »

so it seems  "blah".hashCode() always returns the same thing - if thats true, well then good, although I dont get it
because
1  
2  
"blah".hashCode();
"blah".hashCode();

2 lines of code, 2 strings, 2 different objects, right ? They just happen to have the same string value.

as pjt33 said when you do it like this you have only one object, because string constants get cached.
try new String("blah")
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (12 views)
2014-09-23 14:38:19

radar3301 (13 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!