Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
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 Number?  (Read 2932 times)
0 Members and 1 Guest are viewing this topic.
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Posted 2012-11-07 09:47:29 »

I want to make a game that involves random numbers (between 1 and 2(as in either 1 or 2)) Please help me. (or maybe 1 and 4) Tongue

Smiley
Offline Varkas
« Reply #1 - Posted 2012-11-07 09:57:03 »

Number from 1 to 2:

int number = 1 + (int) (Math.random() * 2.0);

I to 4:

int number = 1 + (int) (Math.random() * 4.0);

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #2 - Posted 2012-11-07 09:58:48 »

Ok, i'l add that in.  Grin

Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Joshua Waring
« Reply #3 - Posted 2012-11-07 10:17:42 »

or
1  
2  
3  
4  
5  
6  
7  
public int randomNumber(int min, int max){
    int random;
    do{
        random = Math.random * max;
    }while(random < min)
    return random;
}

The world is big, so learn it in small bytes.
Offline Varkas
« Reply #4 - Posted 2012-11-07 10:20:52 »

Without loop:

1  
2  
3  
4  
5  
public int randomNumber(int min, int max)
{
    int random = min + (int)(Math.random * (max-min+1));
    return random;
}


if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Phased
« Reply #5 - Posted 2012-11-07 10:21:43 »

even simpler

1  
2  
3  
4  
5  
6  
Random random = new Random(); 
int result_for_two = 1 + random.nextInt(2); //random.nextInt(2) will give either
//0 or 1, so we add 1 to it making 0 = 1 and 1 = 2

//exact same reason as result_for_two we want to get off 0.
int result_for_four = 1 + random.nextInt(4);
Offline Joshua Waring
« Reply #6 - Posted 2012-11-07 10:23:04 »

 

1  
2  
3  
4  
public int randomNumber(int min, int max)
{
    return min + (int)(Math.random * (max-min+1));
}


 

The world is big, so learn it in small bytes.
Offline Phased
« Reply #7 - Posted 2012-11-07 10:24:18 »

battle for the random numbers!

How many different ways can we produce the number!
Offline Roquen
« Reply #8 - Posted 2012-11-07 10:42:02 »

Poorly...many ways!
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #9 - Posted 2012-11-08 07:09:33 »

Do you mean many ways, but poorly? Or poor ways but many ways?

Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 143
Projects: 4
Exp: 3 years



« Reply #10 - Posted 2012-11-09 03:56:03 »

Lolwut
Anyways I think you should Google this stuff first. Or at least learn more Java...I mean, it really wouldn't take you very long to search this up.
Offline Roquen
« Reply #11 - Posted 2012-11-09 10:29:55 »

@masteryoom: I meant there are many ways to poorly generate.   This is it self isn't a problem as poor quality (assuming you don't massively skew the distribution) is more than sufficient virtually always in the context of games.  However there are some "bad habits" which should be avoided IHMO.  I'll try to motive myself to toss together a basic wiki page.
Offline Varkas
« Reply #12 - Posted 2012-11-09 11:01:51 »

If you stick to the API of class Random, I assume you're safe from most pitfalls - I assume tha makers of the class knew what they were doing. And the Java API has RNG implementation which suffice data encrytion standard, that ensures a certain level of quality "randomness".

Just look what methods are there and use them.

Do not mess with the numbers much by yourself unless you have a good understandig of math and what random numbers are about (i.e. which qualities make up a "good" random number, and what srt of calculations will damage those qualities.)

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Roquen
« Reply #13 - Posted 2012-11-09 11:11:54 »

Random is actually an awful design.  Not really a problem if you're single threaded and aren't generating a large number random numbers though.
Offline Varkas
« Reply #14 - Posted 2012-11-09 11:40:45 »

Really? I'm using it to generate all the stellar systems in my "Solarex" project, and it seems to be quite alright. It's single threaded though, and I think it was never claimed that Random is thread safe, so I wouldn't blame it for being not thread safe.

What kind of "awful" did you find there?

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline Pickleninja

JGO Coder


Medals: 10
Projects: 1


I'm tired of working for someone else.


« Reply #15 - Posted 2012-11-09 14:17:25 »

This thread made me lol  Grin


-Pickle

Offline Roquen
« Reply #16 - Posted 2012-11-09 14:49:00 »

@Pickleninja: Threads that make you laugh are awesome.
@Varkas: It is thread safe, that's one of the problems! Smiley  Seriously its main problem is that the design doesn't handle any use-case well.  Be again, nothing to care about it you're only singled threaded.
Offline Varkas
« Reply #17 - Posted 2012-11-09 15:03:05 »

I'm just curious to learn what it doesn't do well. I mostly use the nextInt() and nextLong() methods, and so far I couldn't notice anything bad from the random numbers that these produce. I googled and found a few warning abou the bad quality of java.util.Random but the pages that I saw didn't tell anything precise ... what exactly are the problems? Which use cases are handled badly?

Actually I'm using "rng = new SecureRandom();" if available in my Solarex project since a while because it was said to be a better generator, but I couldn't figure much of a difference in my application.

Since I use RNGs a lot to create all kinds of things I'm quite interested in learning about the problems of java.util.Random so I can make up my mind next time that I need a random number source.

Don't get me wrong, I'm not trying to defend java.util.Random or so, I'm just not aware of the problems.

Edit: I've googled some more, and it basically seems that it implements an older crng algorithm which was "state of the art" when the class was written, but there are better implementations known now, which give better random numbers at less CPU consumption.

I'm not sure how this translated to "It can't handle any use case well", though. It should be good enough for most random number needs.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline pitbuller
« Reply #18 - Posted 2012-11-09 19:13:39 »

1  
2  
3  
4  
5  
static final public int random()
{
  //todo change this to 2
  return 3;
}
Offline sproingie

JGO Kernel


Medals: 202



« Reply #19 - Posted 2012-11-09 19:19:01 »

Oh goody, another random thread.  Rather than debate the finer points, let's boil it down to some do's and don'ts:

  • Don't use the static methods on Random.  Create an instance of Random instead.  If you're saving a seed (e.g. for maps) then this advice isn't really optional.
  • If you don't mind depending on java7, use ThreadLocalRandom.
  • If you need a cryptographic random source, just stop and ask what you're doing first.  If you're generating nonces for a web session, great, use SecureRandom.  If you're trying to actually encrypt something, then consider that if you're expert enough to do crypto from scratch, you should be smart enough not to.  Use a proper crypto implementation like BouncyCastle or Jasypt.

Offline Sickan

Senior Devvie


Medals: 9



« Reply #20 - Posted 2012-11-09 19:27:22 »

1  
2  
3  
4  
5  
static final public int random()
{
  //todo change this to 2
  return 3;
}

Determined by a fair dice toss Cheesy
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #21 - Posted 2012-11-10 00:42:38 »

Oh goody, another random thread.  Rather than debate the finer points, let's boil it down to some do's and don'ts:

  • Don't use the static methods on Random.  Create an instance of Random instead.  If you're saving a seed (e.g. for maps) then this advice isn't really optional.
  • If you don't mind depending on java7, use ThreadLocalRandom.
  • If you need a cryptographic random source, just stop and ask what you're doing first.  If you're generating nonces for a web session, great, use SecureRandom.  If you're trying to actually encrypt something, then consider that if you're expert enough to do crypto from scratch, you should be smart enough not to.  Use a proper crypto implementation like BouncyCastle or Jasypt.


I do mind. I'm on Java 6.  Grin

Smiley
Offline Sammidysam
« Reply #22 - Posted 2012-11-10 01:02:31 »

Why don't you update to Java 7?
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #23 - Posted 2012-11-10 01:54:40 »

I thinking of asking my dad to update my computer. Tongue

Smiley
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 143
Projects: 4
Exp: 3 years



« Reply #24 - Posted 2012-11-10 02:03:45 »

I thinking of asking my dad to update my computer. Tongue
What does this have to do with Java 7?
oh you must be on a non-admin account. carry on Tongue
Offline Sickan

Senior Devvie


Medals: 9



« Reply #25 - Posted 2012-11-10 14:01:22 »

I thinking of asking my dad to update my computer. Tongue
What does this have to do with Java 7?
oh you must be on a non-admin account. carry on Tongue
Or a *gasp* old Mac version!

Cheers! Cheesy
Offline Roquen
« Reply #26 - Posted 2012-11-12 13:48:23 »

A basic summary of java.util.Random (and everything based on it is) is meh:

1) Random is thread safe.  I'd guess that this was chosen to make it general purpose and so people could use it without any PRNG knowledge.  Bad choice IMHO.  You never ever want to call the same generator in more that one thread.  It makes your code non-deterministic.  While that might sound good for random numbers...that's an illusion as it doesn't improve the quality, it just makes your code hard (or near impossible) to debug.  Secondly it means you're hitting the same memory if two concurrent threads are generating at the same time which leads to cache thrashing.  Bad bad.  For ThreadLocalRandom this obviously isn't an issue.  Never use the same generator on more than one thread.  Because of this you never what to use Math.random(), unless single-threaded.  (That call really shouldn't exist IHMO).

2) Neither the method nor the chosen constants were not "state-of-the-art" when it was first written. Far from in fact. My educated guess is it was thrown together in a rush pulling stuff from the then most recent version of Knuth's Art of.. book.  Having said that, it was (and to an extent still is) a popular choice and at the time was a reasonable-ish one.  While for gaming purposes this isn't a big deal, but it's kinda desirable to get the most bang for a given cost.

3) They use a rejection method to get integer results.  This involves a modulo (slow..less so today, but still slow).  The purpose of the rejection method is to improve uniformity of the result (equal probability of all values in the range) when performing the mod (so only for non-power-of-two 'n').  The problems here are that, first mod-power-of-two LCG are poorish quality generators (again, more than sufficient for gaming purposes) and doing a rejection method with this rather silly.  Secondly mod gives higher importance to low-bits, which for mod-power-of-two LCGs are the least random (you want to use high bits).  But the biggest issue is....you don't need to use a modulo anyway.  Using fixed point (or floating point like some of the examples above) multiplication avoids the problem and it's crazy faster.

4) The methods that returned floats was broken (improperly biased) for many versions

So, individually each of these is "meh", but combined in a big palmslap.

OP:  I should have mentioned, I threw this together for simulating die rolls: http://www.java-gaming.org/topics/dice-roller/26974/view.html.

Offline Varkas
« Reply #27 - Posted 2012-11-12 14:33:37 »

Thank you for the detailed explanation. Now I know better why you said, it's bad. Particularly (3) doesn't sound like there were experts at work, indeed.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline sproingie

JGO Kernel


Medals: 202



« Reply #28 - Posted 2012-11-12 18:36:17 »

Everything Roquen says is spot on, but I'd add that j.u.Random is still good enough for your average RPG dice roller.  Just don't use the static methods, and never use it (or any other PRNG) where real money or sensitive info is on the line.

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.

Elsealabs (18 views)
2014-12-28 10:39:27

CopyableCougar4 (20 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (116 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!