mercredi 4 mars 2020

Create a random number in a range from a fixed and variable denominator

From today's date in time() format and a username I need to create a random number between 900 (15 minutes) and 7200 (3 hours) valid for 24 hours.

$timestamps = '1583334801'; // 03/04/2020
$username = 'donaldtrump';

This random number cannot be stored, so it must be calculated several times a day, the result must be identical for 24 hours.

Do you have an idea on how to do this en PHP?




Why is the output of a set constructed from a list of random numbers always ordered?

I noticed that constructing a set from a given list of numbers in order to contain only unique numbers returns always a sorted set. I read that a set is not ordered, so what am I missing here?

I run this simple code a few times, printing always an orderd list:

import random
random_list = [random.randrange(1, 100) for i in range(1000)]
print(set(random_list))



Stratified Sampling to mimic Population Distribution

I am new to R, I recently used stratified sampling for train and test split to ensure target label is in equal proportion for both now I want to use down-sample the training data such that the population distribution/ train distribution is similar to the new down-sample distribution.

The reason I want to down-sample is because I have 11 Million rows with 56 columns and it will take days to do parameter tuning via grid/random/Bayesian search

I am using XGboost and it's is a binary classification problem

I would really appreciate if someone can help me on this.

Below is my code

    train_rows = sample.split(df$ModelLabel, SplitRatio=0.7) ## Stratiefied sampling 
    train = df[ train_rows,]
    test  = df[!train_rows,]`enter code here`



How to create a random number in a range from a fixed and variable denominator [closed]

From today's date in time() format and a username I need to create a random number between 900 (15 minutes) and 7200 (3 hours) valid for 24 hours.

$timestamps = '1583334801'; // 03/04/2020
$username = 'donald_trump';

This random number cannot be stored, so it must be calculated several times a day, the result must be identical for 24 hours.

Do you have an idea on how to do this en PHP?

Thanks for your help.




Getting same values with srand time null in Inheritance class

I am trying to revise what i have learned in class and making a simple Inheritance with "Student" class. Whenever i create a student, it creates StudentStats which is supposed to have random values in constructor as you see in the code. But i always get the same values from constructor. I don't see what is missing, i included time.h and srand(time(null)) is in the constructor.

It may be a beginner problem but i really don't see what s wrong :/

Here is the student class:

#pragma once
#include <iostream>
#include <string>
#include "Personne.h"
#include "PTUT.h"
#include "Marks.h"
#include "StudentStats.h"
#include "StudentClass.h"

using namespace std;

class PTUT;
class StudentClass;
class Etudiant : public Personne, public Marks, public StudentStats

Here is the StudentStats constructor:

#include "StudentStats.h"
#include <iomanip>
#include <time.h>

StudentStats::StudentStats()
{
    srand(time(NULL));
    Morale = (rand() % 81) + 20 ;
    MathLevel = (rand() % 81) + 20;
    ProgLevel = (rand() % 81) + 20;
    NetworkDBLevel = (rand() % 81) + 20;
    GeneralLevel = (rand() % 81) + 20;
}

And the result i get (I create 4 students in main and stats of each student is separeted with "--------"):

Results




random data struct in c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>
#include <stdbool.h>
#include <time.h>
struct Book {
    int BookID;         
    char Title[100];    
    char Author[50];    
    float Price;    
    int YearOfRelease;  
};

struct Book lstBooks[10000]; 
int maxBook = 0; 
int startBookIndex = 10000; 

#define MAX_NUMBER_TITLES 1000
#define MAX_NUMBER_AUTHORS 1000
#define MAX_LENGTH_OF_TITLE 100
#define MAX_LENGTH_OF_AUTHOR 50
char lstTitles[MAX_NUMBER_TITLES][MAX_LENGTH_OF_TITLE];
char lstAuthors[MAX_NUMBER_AUTHORS][MAX_LENGTH_OF_AUTHOR];

void printBookInfo(struct Book b) {
    printf("BookID: %d\n", b.BookID);
    printf("Title: %s\n", b.Title);
    printf("Author: %s\n", b.Author);
    printf("Price: %f\n", b.Price);
    printf("Year of release: %d\n", b.YearOfRelease);
    printf("-------------------------\n");
}

bool checkExistingBook(struct Book b) {

    return false; 

}

void inputBookInfo() {
    struct Book b1;

    if (checkExistingBook(b1) == false) {
        lstBooks[maxBook].BookID = startBookIndex+maxBook;
        strcpy(lstBooks[maxBook].Title, lstTitles[random0_Upper(10)]);
        strcpy(lstBooks[maxBook].Author, lstAuthors[random0_Upper(10)]);
        lstBooks[maxBook].Price = random0_Upper(50);
        lstBooks[maxBook].YearOfRelease = randomLower_Upper(1980, 2020);
        maxBook++; 
    }   
}

bool isCallSRand = false;

int random() {
    if (isCallSRand != true) {
        srand(time(NULL));   
        isCallSRand = true;
    }
    int r = rand(); 
    return r;
}

int random0_Upper(int upper) {
    return (random()%(upper+1));
}

int randomLower_Upper(int lower, int upper) {
    int r = (random() % (upper - lower + 1)) + lower;
    return r;
}




void initData() {
    strcpy(lstTitles[0], "C Programming Language");
    strcpy(lstTitles[1], "The C Programming Language 2nd Edition");
    strcpy(lstTitles[2], "C Programming: A Modern Approach, 2nd Edition 2nd Edition");
    strcpy(lstTitles[3], "Expert C Programming: Deep Secrets 1st Edition, Kindle Edition"); 
    strcpy(lstTitles[4], "C: The Complete Reference, 4th Ed. 4th Edition");
    strcpy(lstTitles[5], "Head First C 1/e Edition");
    strcpy(lstTitles[6], "Computer Fundamentals and Programming in C");
    strcpy(lstTitles[7], "Low-Level Programming: C, Assembly, and Program Execution");
    strcpy(lstTitles[8], "C in a Nutshell: The Definitive Reference 2nd Edition");
    strcpy(lstTitles[9], "Hands-on Network Programming with C: Learn socket programming in C and write secure and optimized network code");
    strcpy(lstTitles[10], "Data Structures Using C 2nd Edition");

    strcpy(lstAuthors[0], "James Gosling");
    strcpy(lstAuthors[1], "Peter Norton");
    strcpy(lstAuthors[2], "Marry Curry");
    strcpy(lstAuthors[3], "Tony Crawford"); 
    strcpy(lstAuthors[4], "Igor Zhirkov");
    strcpy(lstAuthors[5], "Peter Van der Linden");
    strcpy(lstAuthors[6], "Kernighan");
    strcpy(lstAuthors[7], "Adam Riches");
    strcpy(lstAuthors[8], "Dennis Ritchie");
    strcpy(lstAuthors[9], "Jonathan Payne");
    strcpy(lstAuthors[10], "John Smith");

}





int main(int argc, char *argv[]) {



    int j=0;


    initData();

    for (j=0; j<10; j++) 
        inputBookInfo();

    for (j=0; j<10; j++) 
        printBookInfo(lstBooks[j]);


    return 0;
}

This code will generate a random list of books in library,but i am stuck at writing the function "checkExistingBook()"-if the book has been already in library then return false,else return true if the book isn't in library,just in case the random data repeats itself like 2 books about "C Programming Language"




What is a set synchronized CSPRNG?

I'm looking to implement a performant revocation method for JWTs. I'm reading a paper, in which the following section's second paragraph states:

If we use the client hashing approach described previously, we have a greater volume of keys to deal with. One possible approach would be to generate the secret using a set synchronized cryptographically secure pseudorandom number generators, creating a kind of rolling code for each group of clients. This would mean that only the key change events have to propagate, not the actual keys themselves, greatly reducing the associated performance cost.

What is a set synchronised cryptographically secure pseudorandom number generator (CSPRNG)? I know what a CSPRNG is, in that it is a random number generator with an acceptable degree of randomness. But what is the set synchronised part? Does it relate to synchronised sets in Java? What does being set synchronous achieve in this context?