lundi 2 mars 2020

Confused by Error: object 'carryover' not found

I want to sample a large data set of 100,000 rows and do a calculation with each of 10,000 random values chosen. The sampling chooses a random value called "unused", and based on the value chosen there are three choices using if statements, but for some reason the variable "carryover" is not found.

Code is:

options(width=10000)
unused <- round(runif(100000, 0, 600),0)

B <-10000
unused <-matrix(sample(unused,size=B,replace=TRUE),nrow=B)
for (i in 1:B){
  if (unused[i]==0){
    deduct[i] <- 0
    carryover[i] <- 0
    used[i] <- 0
    if (unused[i] > 200)
      deduct[i] <- unused[i]-200
    carryover[i] <- 200
    used[i] <- carryover[i]+400
    if (unused[i] >0 & unused[i] <= 200)
      deduct[i] <- 0
    carryover[i] <- unused[i]
    used[i] <- carryover[i]+400
  }
}
View(data.frame(unused,deduct,carryover,used))



C++ Insert a random string into multiple places of another string using a loop

I wrote this code at the bottom and I want it to insert a random string r into my string str and multiple set intervals or at multiple random intervals. It is very clunky and I will need to increase it to say 1000 chars otherwise it won't really fix my problem. Is there anyone who can help me to put this into a loop? I want to use the length of my string to determine the size of the loop and a set 15 char increase in my random insert function. I have tried a loop and it was unsuccessful.

int main() {
string str;
int len;
int randNum;
char x;
char x1;
char x2;
char x3;
char x4;
char x5;
char x6;
string str1;
string str2;
string str3;
string str4;
string str5;
string str6;

str = "This is just a random string of text! I have extended it just to test my code.";

//works great
char letters[] = "abcdefghijklmnopqrstuvwxyz";
srand(time(0));

len = str.length();
randNum = len/2;

cout << str << endl;
cout << len << endl;
cout << randNum << endl;

if (randNum >= 100){     // Need help with using a loop for this if function
  char x = letters[rand() % 26];
  char y = letters[rand() % 26];
  char z = letters[rand() % 26];
  string r;
  r = ".";
  r += x;
  r += y;
  r += z;
  str.insert(randNum % 10 + 100, r);
  str1 = str;
}
else{
  str1 = str;
}
  if (randNum >= 85){
    char x1 = letters[rand() % 26];
    char y1 = letters[rand() % 26];
    char z1 = letters[rand() % 26];
    string r2;
    r2 = ".";
    r2 += x1;
    r2 += y1;
    r2 += z1;
    str1.insert(randNum % 10 + 85,r2);
    str2 = str1;  
  }
  else{
    str2 = str1;
  }
    if (randNum >= 60){
      char x2 = letters[rand() % 26];
      char y2 = letters[rand() % 26];
      char z2 = letters[rand() % 26];
      string r3;
      r3 = ".";
      r3 += x2;
      r3 += y2;
      r3 += z2;
      str2.insert(randNum % 10 + 60,r3);
      str3 = str2;
    }
    else{
      str3 = str2;
    } 
      if (randNum >= 45){
        char x3 = letters[rand() % 26];
        char y3 = letters[rand() % 26];
        char z3 = letters[rand() % 26];
        string r4;
        r4 = ".";
        r4 += x3;
        r4 += y3;
        r4 += z3;
        str3.insert(randNum % 10 + 45,r4);
        str4 = str3;
      }
      else{
        str4 = str3;
      }
        if (randNum >= 30){
          char x4 = letters[rand() % 26];
          char y4 = letters[rand() % 26];
          char z4 = letters[rand() % 26];
          string r5;
          r5 = ".";
          r5 += x4;
          r5 += y4;
          r5 += z4;
          str4.insert(rand() % 10 + 30,r5);
          str5 = str4; 
        }
        else{
          str5 = str4;
        }
          if (randNum >= 15){
            char x5 = letters[rand() % 26];
            char y5 = letters[rand() % 26];
            char z5 = letters[rand() % 26];
            string r6;
            r6 = ".";
            r6 += x5;
            r6 += y5;
            r6 += z5;
            str5.insert(rand() % 10 + 15,r6);
            str6 = str5;
          }
          else{
            str6 = str5;
          }
            if (randNum >= 0){
              char x6 = letters[rand() % 26];
              char y6 = letters[rand() % 26];
              char z6 = letters[rand() % 26];
              string r7;
              r7 = ".";
              r7 += x6;
              r7 += y6;
              r7 += z6;
              str6.insert(rand() % 10,r7);
            }
            else{
              return 0;
            }

cout << str6 <<endl;



dimanche 1 mars 2020

How can I assign the openssl rand -hex 8 query to a variable with windows command line (cmd)?

How can I assign the openssl rand -hex 8 query to a variable with windows command line (cmd)?

set randomString=openssl rand -hex 8

echo %randomString%

Not Work.




I'm trying to display multiple random non repeating images from a folder but I get blank images

Hi I'm totally new at php and I put this script together modifying other scripts ... I'm trying to display multiple random non repeating images from a folder and it basically works except sometimes instead of an image I get a "blank" anyone can explain me why?

<?php

$imagesDir = 'images/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$imageList = range(0,count($images));
shuffle($imageList);
$image_idx = 0;

        echo'
<div class="main">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
</div>';

echo'
<div class="main">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
</div>';

echo'
<div class="main">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
</div>';

echo'
<div class="main">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
</div>';

echo'
<div class="main">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
</div>';

echo'
<div class="main">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
</div>';

echo'
<div class="main">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
<img src="' . $images[$imageList[$image_idx++]]. '" style="height:auto;width:33.33%;float: left">
</div>';
?>



C++ rand() returns nonsensical values on first run, works fine on second

I am in the middle of making a lottery program and this is my randomization function

#include <iostream>
#include <stdlib.h>
#include <time.h>// for seeding srand
#include <algorithm> //for std::find
#include <iterator> //for std::begin, std::end

using namespace std;

void rivinArvonta(int arvottuRivi[]){   
    int rivi[7];
    int pallo = 41;
    srand(time(NULL));

    for (int i = 0; i < 7; i++) {
            pallo = rand() % 40 + 1;
            bool exists = (find(begin(rivi), end(rivi), pallo) != end(rivi));
            if (exists == false) {
                    rivi[i] = pallo;
            }else
                    i = i - 1;
    }
    sort(rivi, rivi + 7);


    for (int i = 0; i < 7; i++){
        arvottuRivi[i] = rivi[i];
    }
}

The program runs in a while loop until I tell it to stop. The first time I call this function, it returns something like 5, 0, -164820691, 21983, 0, 0, -164820768. The second time I call the function it works just as intended.

I don't understand how pallo = rand() % 40 + 1; can return something so much out of the bounds? Could this be because I use g++?

PS sorry for having my variable names in finnish.




My class objects with randomised values get the exact same values

I'm trying to create multiple objects with different randomised values with a structure from a JSON file. For every new object I create I, for some reason, get the same randomised values as the first object.

I'm doing this by creating an object through a class. I put every "Thingy" created in an array for future reference. The JSON file is populated with attributes that should, everytime a new Thingy is created, each get a random value from 0-10. Classes is not my strong side, I'm happy to hear better ways to do this (I'm interested in having funtions inside the Thingies later on).

When I reload the page and create a new Thingy, by pressing my button, the values are beautifully randomised. The problem is that every time I press my button to create more Thingies the name is randomised BUT every value gets the same random values as the previous Thingy. At first I thought is was a problem with Javascript using a pseudo random number generator but now I believe I have some wrong understanding on a script language level.

Can anyone shine some light on what I'm doing wrong or not understanding?

Please tell me if I should provide any more information, this is my first post!

CODE:

var i;
var names = ["bjorn", "agnetha", "benny", "annifrid"];
var thingies = [];

var jsonFile = {
  "bodyA": {
      "sub1": {
          "subsub1": null,
          "subsub2": null,
          "subsub3": null
      },
      "sub2": {
          "subsub1": null,
          "subsub2": null
      },
      "sub3": {
          "subsub1": {
              "subsubsub1": null,
              "subsubsub2": null,
              "subsubsub3": null
          }
      }
  },
  "bodyB": {
      "sub1": {
          "subsub1": null
      }
  },
  "bodyC": null
}

class Thingy {
  constructor (name, attributes) {
      this.name = name;
      this.attributes = attributes;
  }
}

var button = document.createElement("input");
  button.type = "button";
  button.value = "Create new thingy";
  button.onclick = function() {
      // I'm using the math.js to get a random value from the names array (Yes, in this debugging instance it could get the same name as other Thingies)
      thingy = new Thingy(math.pickRandom(names), getAttributes(jsonFile));
      thingies.push(thingy);
      console.log(thingy.name + " was created!");
  }
  document.getElementById("myDiv").appendChild(button);

function getAttributes(json) {
  function iterateObject(json) {
      Object.keys(json).forEach(function (k) {
          if (json[k] !== null && typeof json[k] === 'object') {
              iterateObject(json[k]);
              return;
          }
          if (json[k] === null) {
              // This is where I get the exact same values everytime I create a new Thingy after the first one
              json[k] = math.randomInt(0, 10); // (Also from math.js - very similiar to default Math)
          }
      });
      return json;
  }
  return iterateObject(json);
}

function logThingies() {
  for (i = 0; i < thingies.length; i++) console.log(thingies[i]);
}
<head>
    <!-- math.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.6.1/math.js"></script>
</head>
<body>
    <div id="myDiv">
        <button onclick="logThingies()">Show me all!</button>
    </div>
</body>



Cryptographically secure RNG in C++ for RSA PKCS#1 (key generation)

I'm trying to re-implement the RSA key generation in C++ (as a hobby/learning playground) and by far my biggest problem seems to be generating a random number in range x,y which is also cryptographically secure (the primes p and q, for example).

I suppose using mt19937 or std::rand with a secure random seed (e.g. /dev/urandom or OpenSSL RAND_bytes etc) would not be considered 'cryptographically secure' in this case (RSA)?

ISAAC looked promising but I have zero clue on how to use it since I wasn't able to find any documentation at all.

Notably, this is also my first C++ project (I've done some C, Rust etc before... So C++ at least feels somewhat familiar and I'm not a complete newbie, mind you).