Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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  
  Never-ending Shuffled Sequences - When Random is too Random  (Read 3171 times)
0 Members and 1 Guest are viewing this topic.
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Posted 2008-02-03 22:52:56 »

Shameless pluggage ahead:
http://kaioa.com/node/53

Note: It's massive! Smiley

弾幕 ☆ @mahonnaiseblog
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #1 - Posted 2008-02-04 15:27:36 »

Thanks for the link, it is really insteresting. I will take a closer look to it later.
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #2 - Posted 2008-02-04 17:47:05 »

Nice! That's pretty elegant.

I'm not so sure I'd want to use randomness like that in all games, though.. take diablo, for example. If you knew that killing diablo one hundred times would result in at least one Drop of Special Value(tm), it'd feel less like a lucky drop, and more like just grinding your way through a deck of cards until you find the one you're looking for.

Play Minecraft!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2008-02-04 18:52:06 »

In a multiplayer environment you could dodge this issue by using a single global ShuffleBag for this kind of thing.

Well, it's just another nice tool to have. It certainly isn't the ultimate solution for any randomness problem. But for those problems it can be used for it's very handy.

edit: You can of course also multiply the weights eg 2/198 or 10/990 instead of 1/99.

弾幕 ☆ @mahonnaiseblog
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2008-02-05 07:48:39 »

But for those problems it can be used for it's very handy.

Absolutely! As I said, you've got a very elegant implementation there, solving a problem I had never fully thought about, so I might end up using it in my next game. =)

Play Minecraft!
Offline qgpr

Senior Newbie




yawn


« Reply #5 - Posted 2008-04-14 17:37:57 »

After reading your blog I came up with this idea in which you'd need double or float probabilities for perhaps extremely low chances of some items drops (like in diablo 2), my idea was that you add items with a percentage probability (0.0-1.0), but if you end up going past 1.0d it would still work.

It isn't true random but if we go by the premise, true random is not the goal anyway. Only thing is that I am not good with benchmarks, I bet this would be better for huge number of items with extremely low probabilities (as keeping an array spot for each chance would eat memory), but I have no idea which would be better for less extreme cases.

Coded it in notepad bear with that.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
ShuffleBag
{
   private java.util.ArrayList< Item > items;
   
   long drawPeriod;
   long drawsLeft;
   
   public class Item
   {
      public Item( String name, double percentage )
      {
         item = name;
         weigth = percentage;
         drawsLeft = 0;
         timesToDraw = 0;
         doneDrawn = true;
      }
      public void setTimesToDraw( long timesToDraw )
      {
         drawsLeft = timesToDraw + this.timesToDraw - drawsLeft; // timesToDraw - ( this.timesToDraw + drawsLeft );
         this.timesToDraw = timesToDraw;
         
         doneDrawn = ( drawsLeft <= 0 );
      }
      public drawn()
      {
         --drawsLeft;
         if( drawsLeft <= 0 ) doneDrawn = true;
      }
      public resetDraws()
      {
         drawsLeft = timesToDraw;
         doneDrawn = ( drawsLeft <= 0 );
      }
     
      String item;
      double weight;
      long drawsLeft;
      long timesToDraw;
      boolean doneDrawn;
   }
   
   addItem( String name, double percentage, boolean pack )
   {
      Item item = new Item( name, percentage );
     
      items.add( item );
      if( pack ) pack();
   }
   pack()
   {
      double totalWeigth = 0;
      double minWeight = Double.MAX_VALUE;
      for( Item i : items )
      {
         totalWeigth += i.getWeight();
         if( minWeight > i.getWeight() ) minWeight = i.getWeight();
      }
     
      double leftoverWeight = 0.0d;
      if( totalWeight < 1.0d )
      {
         leftoverWeight = 1.0d - totalWeight;
         totalWeight = 1.0d;
      };
     
      drawPeriod = 0;
      for( Item i : items )
      {
         long timesToDraw = Math.ceil( i.getWeight() / minWeight );
         i.setTimesToDraw( timesToDraw );
         drawPeriod += timesToDraw;
      }
     
      drawPeriod += Math.ceil( leftoverWeight / minWeight );
      drawsLeft = drawPeriod;
   }
   
   String drawNext()
   {
      boolean reset = false;
      if( drawsLeft <= 0 )
      {
         drawsLeft = drawPeriod;
         reset = true;
      };
     
      long rand = RNG.getLong( 0, drawsLeft );
     
      boolean found = false;
      String s = "";
      long k = 0;
      for( Item i : items )
      {
         if( reset ) i.resetDraws();
         if( found ) continue;
         if( i.isDoneDrawn() ) continue;
         
         k += i.getDrawsLeft();
         if( rand < k )
         {
            s = i.getItem();
            i.drawn();
            --drawsLeft;
            found = true;
            if( !reset ) break;
         }
      }
     
      return s;
   }
}



Hmm thinking about it it would still have the problem where you get several times a low or high random number, perhaps the item list could be shuffled from time to time so the check order changed.
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.

DarkCart (15 views)
2014-10-31 21:44:48

DarkCart (19 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (30 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
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

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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!