Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Algorithm RNG that progressively gets harder the higher the number (https://forums.graalonline.com/forums/showthread.php?t=134268656)

Cubical 08-23-2013 12:35 PM

Algorithm RNG that progressively gets harder the higher the number
 
I currently need a way to create random number generation that gets progressively harder the higher the number..

example:
You have the chance of getting a number 0-10.

You are most likely to get 1, less likely to get 2, even less likely to get 3 and so on.

Does anyone have any suggestions on how to do where it would be progressively harder each number you go up or would my best bet be to do a bunch of logic checks to see if a random number is within a set range and iteration lessen the the gap that allows it to proceed to the next iteration until I've hit the whatever the max number?

DrakilorP2P 08-23-2013 03:15 PM

Sounds a bit like "normal distribution", or "the bell curve." You can generate numbers that act like that using the "Box–Muller transform." Look up these things.

My attempt at implementing it in GScript:
PHP Code:

function normal() {
  
temp.2.7182818284590452353602874713526624977572470936999;
  return 
cos(2*pi*random(01)) * (-log(temp.erandom(01)))^0.5;


It generates numbers that are more likely to be close to 0.

If you know in advance that you only need numbers 1 through 10 it's probably easier to do it another way.

cbk1994 08-23-2013 04:03 PM

Quote:

Originally Posted by DrakilorP2P (Post 1721940)
Sounds a bit like "normal distribution", or "the bell curve." You can generate numbers that act like that using the "Box–Muller transform." Look up these things.

This is what we did on Kingdoms for the mystery boxes (for determining which item you got). If you need more code samples, I can dig up our implementation as well.

Cubical 08-23-2013 04:14 PM

I'll take a look at it when I get home and see if I can get a general understanding on what he provided and if not I'll take you up on that probably.

Loriel 09-01-2013 03:03 AM

wow i'm bad with statistics. apparently you can transform your rand()'s uniform distribution into a desired distribution via the latter's quantile function which is the inverse of the cumulative distribution function which is the integral of the probability density function and you could pick something like 2*(1-x) as your density function (for 0<=x<1, i guess the idea is that it's normalized). then wolfram alpha gives 1-sqrt(1-x) (the + solution sounds useless i dunno). so you'd transform the uniform 0<=x<1 rand() into a number between 0 and 10 by doing (1-sqrt(1-rand()))*11 and rounding down.

i guess if 1-x doesn't appeal to you as a distribution you can do the whole thing with stuff like 1-sqrt(x) and 1-x*x and that sort of thing and see if something cool happens but wow i hate statistics.

Crono 09-01-2013 11:00 AM

Quote:

Originally Posted by Loriel (Post 1722155)
wow i'm bad with statistics. apparently you can transform your rand()'s uniform distribution into a desired distribution via the latter's quantile function which is the inverse of the cumulative distribution function which is the integral of the probability density function and you could pick something like 2*(1-x) as your density function (for 0<=x<1, i guess the idea is that it's normalized).

wat

xXziroXx 09-01-2013 01:02 PM

Good seeing you as usual Loriel.

dylan 09-02-2013 02:36 AM

I was trying to do something to where the chance exponentially gets higher or lower, but it seems the Math.pow(int, int) function no longer is in working fashion.

fowlplay4 09-02-2013 03:18 AM

going off loriel's post... I came up with:

PHP Code:

function onCreated() {
  
temp.tmax 10;
  
temp.rand = function (m) { return int(((1-random(0,1)^0.5)) * m) };
  
  for (
temp.0temp.1000temp.i++) {
    
temp.("count_" temp.rand(temp.tmax))++;
  }
  for (
temp.0temp.temp.tmaxtemp.i++) {
    echo(
temp.i SPC temp.("count_" temp.i));
  }
}

/*
Sample:
0 179
1 170
2 145
3 126
4 115
5 95
6 75
7 59
8 27
9 9
*/ 

one other way I tried:

PHP Code:

function onCreated() {
  
temp.tmax 10;
  
temp.rand = function (m) {
    
temp.0;
    while (
random(0,1) > 0.5 && temp.m) {
      
temp.i++;
    }
    return 
temp.i;
  };
  
  for (
temp.0temp.1000temp.i++) {
    
temp.("count_" temp.rand(temp.tmax))++;
  }
  for (
temp.0temp.temp.tmaxtemp.i++) {
    echo(
temp.i SPC temp.("count_" temp.i));
  }
}

/*
Sample:
0 520
1 243
2 132
3 56
4 27
5 12
6 2
7 3
8 3
9 1
*/ 

I'm sure there's a math way to do it but **** it.

cbk1994 09-02-2013 03:23 AM

Quote:

Originally Posted by dylan (Post 1722187)
I was trying to do something to where the chance exponentially gets higher or lower, but it seems the Math.pow(int, int) function no longer is in working fashion.

You can simply use a caret in GScript—it's not used for xor like it is in most languages.

scriptless 09-02-2013 03:43 PM

This sounds interesting. Would defiantly make the daily freebies system more efficient. Is this also used in something like rarity of item spawns/drops?

Loriel 09-02-2013 04:30 PM

Quote:

Originally Posted by fowlplay4 (Post 1722188)
going off loriel's post... I came up with:

PHP Code:

(1-random(0,1)^0.5


It took me way too long to figure out why 1-sqrt(rand()) is the same thing as 1-sqrt(1-rand()) :(


All times are GMT +2. The time now is 07:54 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.