mardi 1 septembre 2015

Initialize multiple rngs in a single program

I want to use multiple RNGs in my C++ application using <random>. What is the best way to seed them? I fear that the random numbers from different RNGs will be correlated too much when seeding each RNG with a random number from a single RNG.




How to make a Button randomly Move in a Layout

Ok so in here i have this xml like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:ads="http://ift.tt/GEGVYd"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:layout_margin="30dp"
         android:background="@drawable/backgroundblankred" >

         <Button
             android:id="@+id/button1"
             android:layout_width="100dp"
             android:layout_height="100dp"
             android:background="@drawable/eeclevelfirstblackbuton" />

     </RelativeLayout>

</RelativeLayout>

it is show like this

enter image description here

the button was inside a layout so i want to make the button when clicked it will be like this

enter image description here

the button randomly move inside the layout there and not out from the layout that can make app force close

Second question, how to set the fast of random button move? like set the buton random move every 2 second and increase it more fast, it is possible set the button move faster?

i just have this code now

public class tested extends Activity {

Button buttonblack;
int score=0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.tested);


    buttonblack = (Button)findViewById(R.id.black1);
    buttonblack.setOnClickListener(new View.OnClickListener() {

              public void onClick(View v){
             //If clicked, Move the button randomly
                                         }                
            });

Anyone can help with some code? Thanks in Advance.




Code give a black page [duplicate]

This question already has an answer here:

i just made a very simple and probobly not so balanced and not so good code. It works like this, user in my webpage page press button it take some amount of coins from him and then in exchange give a random item in random amount to user. All works just good but i have a problem that sometimes it gives a blank page and coins are still taken from users but they wont get any in exchange. Maybe too much rand functions ?

Maybe someone can give me sample how to make it more balanced and fix black page bug.

Thank you and sorry for my English.

Code::

function buyblackbox(){
global $USER, $PLANET, $LNG, $UNI, $CONF,$resource,$pricelist,$Message;
$blackbox = HTTP::_GP('blackbox', 0);
$price = 10000 * $blackbox;
$timer = time();
/////
$l = mt_rand(1,9);
$i = mt_rand(1,9);
$loli   = max($l,$i);
/////
$dmatter = mt_rand(145000,210000);
///
$o = mt_rand(1,15);
$n = mt_rand(1,15);
$oneal = min($o,$n);
///
/////
$x = mt_rand(110,300);
$y = mt_rand(110,300);
////
$m32 = min($x,$y);
$canyon = mt_rand(18,50);
$emiter = mt_rand(30,70);
//////
/////
$w = mt_rand(158,280);
$z = mt_rand(158,280);
$academy = min($w,$z);
////
$x = mt_rand(150,500);
$y = mt_rand(150,500);
$pexp = min($x,$y);
///////
$x = mt_rand(150,400);
$y = mt_rand(150,400);
$cexp = min($x,$y);
///
$userIdLocal=$USER['id'];
$userIdPlanet=$PLANET['id'];
if ($price < 0) {
$this->printMessage("Hack attempt.", true, array('game.php?page=premium', 2));
}
if($USER['antimatter'] < $price){
$this->printMessage("You do not have enough antimatter", true, array('game.php?page=premium', 2));
die();
}
elseif($blackbox < 0){
$this->printMessage("Hack attempt.", true, array('game.php?page=premium', 2));
die();
}else{
    $USER['antimatter'] -= $price;
$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET `darkbox` = '".$loli."', `antimatter` = `antimatter` - '".$price."' WHERE `id` = ".$userIdLocal.";");
if($USER['darkbox'] == 9)
{
$GLOBALS['DATABASE']->query("Update ".USERS." SET  `experience_peace` = `experience_peace` + '".$pexp."' WHERE `id` = ".$USER['id']." ;
"); 
$this->printMessage("You have won ".$pexp." peace experiance points", true, array('game.php?page=premium', 2));
}
if($USER['darkbox'] == 8)
{
$GLOBALS['DATABASE']->query("Update ".USERS." SET  `experience_combat` = `experience_combat` + '".$cexp."' WHERE `id` = ".$USER['id']." ;
"); 
$this->printMessage("You have won ".$cexp." combat experiance points", true, array('game.php?page=premium', 2));
}
if($USER['darkbox'] == 2)
{
$USER['darkmatter'] += $dmatter;
$this->printMessage("You have won ".$dmatter." units of Dark Matter ", true, array('game.php?page=premium', 2));
}
if($USER['darkbox'] == 3)
{
$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET  `M32` = `M32` + '".$m32."' WHERE `id` = ".$userIdPlanet.";");  
$this->printMessage("You have won ".$m32." units of M32 Heavy Class battle ships", true, array('game.php?page=premium', 2));
}
if($USER['darkbox'] == 4)
{
$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET  `bs_class_oneil` = `bs_class_oneil` + '".$oneal."' WHERE `id` = ".$userIdPlanet.";");  
$this->printMessage("You have won ".$oneal." units of Battleship class ONill  ", true, array('game.php?page=premium', 2));
}
if($USER['darkbox'] == 5)
{
$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET  `canyon` = `canyon` + '".$canyon."' WHERE `id` = ".$userIdPlanet.";");
$this->printMessage("You have won ".$canyon." units of Canyon defense ", true, array('game.php?page=premium', 2));
}
if($USER['darkbox'] == 6)
{
$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET  `particle_emitter` = `particle_emitter` + '".$emiter."' WHERE `id` = ".$userIdPlanet.";");
$this->printMessage("You have won ".$emiter." units of Particle Emitter  ", true, array('game.php?page=premium', 2));
}
if($USER['darkbox'] == 7)
{
$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET  `academy_p` = `academy_p` + '".$academy."' WHERE `id` = ".$userIdLocal.";");
$this->printMessage("You have won ".$academy." Academy points", true, array('game.php?page=premium', 2));
}
//////GRAND PRICE /////
if($USER['darkbox'] == 1)
{
$USER['antimatter'] += 80000;
SendSimpleMessage(1, '', $timer, 4, 'System', 'Grand Price', 'User have won grand price. <br> Grand Price user ID  '.$userIdLocal.' account.');
$this->printMessage("<h2>!!!YOU HAVE WON THE GRAND PRICE 80.000 ANTIMATTER!!!<h2>", true, array('game.php?page=premium', 2));
}
}
die();
}




Generating random string of seedable data

I'm looking for a way to generate a random string of n bytes in Python in a similar way to os.urandom() method except providing a way to seed the data generation.

So far I have:

def genRandData(size):
    buf = chr(random.randint(0,255))
    for i in range(size-1):
        buf = buf + chr(random.randint(0,255))
return str(buf)

However this function is very slow, generating a megabyte of data takes about 1.8 seconds on my machine. Is there any way of improving this (or alternatively a way to seed os.urandom).




Is there a C++11 CSPRNG?

As we know, the Mersenne Twister is not crytographically secure1:

Mersenne Twister is not cryptographically secure. (MT is based on a linear recursion. Any pseudorandom number sequence generated by a linear recursion is insecure, since from sufficiently long subsequence of the outputs, one can predict the rest of the outputs.)

But many sources, like Stephan T. Lavavej2 and even this website3. The advice is almost always (verbatim) to use the Mersenne Twister like this:

auto engine = mt19937{random_device{}()};

They come in different flavors, like using std::seed_seq or complicated ways of manipulating std::tm, but this is the simplest approach.

Even though std::random_device is not always reliable4:

std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

The /dev/urandom vs /dev/random debate rages on5.

But while the standard library provides a good collection of PRNGs, it doesn't seem to provide any CSPRNGs. I prefer to stick to the standard library rather than using POSIX, Linux-only headers, etc. Can the Mersenne Twister be manipulated to make it cryptographically secure?

Links

  1. <http://ift.tt/1JJhnsb;

  2. rand() Considered Harmful, Going Native 2013

    <http://ift.tt/1NUKcYY;

  3. Random number generation in C++11 , how to generate , how do they work?

    <http://ift.tt/1JJhnsh;

  4. <http://ift.tt/1JJhlR5;

  5. Myths about /dev/urandom

    <http://ift.tt/1JJhlR7;




Control over random value

I use a random for browse list I do like this:

var oServer = new SmtpServer("");
var random = new Random();
var randomIndex = random.Next(list.Count());
var socks = list[randomIndex];

oServer.SocksProxyServer = socks.IpAddress.ToString();
oServer.SocksProxyPort = socks.Port;

I want that if value selected, will be not selected again in next. I want that it choose another value.

What I should do?




PHP rand function generates wrong values

I've got a little issue with the php function rand. Array is an array with numbers between 0.1 and 1.

 $min = $this->array[0]*10;
 $max = $this->array[1]*10;
 array_push($this->motor, rand($min, $max), rand($min,$max));

Once in a while rand generates 0 the rest of the time the rand function works fine. Does somebody knows what I'm doing wrong?