dimanche 31 décembre 2023

How does Javascript convert the result of xorshift128+ to a float value between 0 and 1 for Math.random()?

I need to write a program exploiting cryptographic insecurity of javascript's Math.random() for a CTF thing. However I do not know how javascript converts an int to a float between 0 and 1. The pseudo-randomization algorithm of javascript, as far as I understand is xorshift128+, which in python would look something like this:

def cast_to_int32(x):
    return x & 0xffffffff


def xorshift128plus():
    global state0
    global state1
    state0 = cast_to_int32(cast_to_int32(18030 * (state0 & 0xffff)) + cast_to_int32(state0 >> 16))
    state1 = cast_to_int32(cast_to_int32(30903 * (state1 & 0xffff)) + cast_to_int32(state1 >> 16))
    return cast_to_int32(cast_to_int32(state0 << 16) + (state1 & 0xffff))

However bitwise operations in this scenario are performed on integers, not floats. But the result of javascript's Math.random() is a float between 0 and 1. How does javascript convert that? I know the most intuitive thing to suggest would be a simple division by 2^32 or 2^31 but I've tried that and couldn't reproduce the results.

I tried dividing the int result by (2^31-1),(2^31),(2^32-1),(2^32) but none of these reproduced the results of Math.random() from the same state.




samedi 30 décembre 2023

How can i pass a random number generator as argument to a function that will produce several different numbers inside the function? [duplicate]

So, I have created several different random number generator functions (rng) that produce values of a random variable X which follow different distribution functions. Now, I want to experiment with the central limit theorem, and have a function (x_n) that produces n different values of X for a given distribution function, so, given the distribution function, I want to produce several different values of X, and then find their mean value.

I tried to just define a function that takes as arguments the number of values I want to generate and the function f (the rng) that I want the variable X to follow. I tried to call the RNG inside the function several times, but, of course, when I put the predefined distribution function as an argument into the x_n(n,rng), it generates only one random number, into the argument section, and then this random number is used throughout the entire run of x_n.

How can I call the rng many times inside the function x_n(n,rng) and produce many different numbers inside of it?

import random as r

#def uniform():
    #return r.random()

def x2():
    u=r.random()
    return u**(1/3)

def x_n(n,f):
    x_n=0
    for i in range(n):
        rand=f
        x_n+=rand
        #print(rand)
    return x_n/n

x_n(5,x2())
x_n(7,r.random())
x_n(20,uniform())

P.S.: One idea I had was to create random number generators that take an argument and just change the argument inside x_n()'s loop, but I was hoping there was another way, so that I can pass just r.random() as the distribution function.




lundi 18 décembre 2023

Restricted C Struct Randomizer

I am attempting to randomize members of a C struct with a few difficulties:

  1. The randomized struct may not be larger than the original one
  2. Each member has an alignment which has to be respected
  3. Members are of different sizes obviously
  4. Some members are fixed in place
  5. Members may not overlap with respect to their sizes
  6. Each member has a range in which it has to end up [min offset, max offset]

I freestyled a randomizer that respects properties 1 to 5. Also taking into account property 6 greatly complicates things. Is there an algorithm that does this kind of thing or could be abused to do it?

For context:

I have this struct representing the properties of a member of a C struct:

struct Member {
    uint32_t size;
    uint32_t offset;
    uint32_t alignment;
    uint32_t maxadd; // Max increase of offset
    uint32_t maxsub; // Max decrease of offset
};

My randomizer gets a list of objects of this struct, one object per member. It is supposed to find new, randomized offsets for each member, while not having any members overlap in the end and not translating a member by more than maxadd/maxsub.

I am not trying to randomize the strings in a struct definition. I am trying to randomize the memory layout itself. Because it is supposed to work on fully compiled binaries, the resulting layout may not consume more memory than the original struct (e.g. by translating a member by a million bytes). My randomizer also knows the size of the original struct.




Distributing random numbers with multiple conditions

My students handed in group projects. Now they are supposed to give feedback to other student's works. Each student should give feedback to two other student projects. I want to assign these projects randomly. Two conditions have to be met:

  1. A student should not give feedback to their own project.
  2. A student should not give feedback to the same project twice.
#Example Data
group <- c(1,1,1,2,2,3,3,3,4,4)
name <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
feedback1 <- c(1,1,1,2,2,3,3,3,4,4)
feedback2 <- c(1,1,1,2,2,3,3,3,4,4)
df <- data.frame(group, name, feedback1, feedback2)

As you can see, the groups are not the same size (some groups have 2 members, some have 3). I created a loop that checks if someone is about to give feedback to himself. If so, a random number is chosen from all the group numbers, until a fitting number is chosen. One occurrence of the chosen number is then removed from the group numbers and the next student is checked.

# Fill feedback1
for (i in 1:nrow(df)) {
  while (df$feedback1[i] == df$group[i]) {
    df$feedback1[i] <- sample(group, 1)
  }
  group <- group[-match(df$feedback1[i], group)]
}

This works so far:


   group name feedback1 feedback2
1      1    a         2         1
2      1    b         3         1
3      1    c         3         1
4      2    d         1         2
5      2    e         1         2
6      3    f         4         3
7      3    g         4         3
8      3    h         2         3
9      4    i         1         4
10     4    j         3         4

If I add the second condition ("students should not give feedback to the same groups") the loop fails to work:

# Fill feedback2
group <- c(1,1,1,2,2,3,3,3,4,4)

for (i in 1:nrow(df)) {
  while (df$feedback2[i] == df$group[i] & df$feedback1[i] == df$feedback2[i] ) {
    df$feedback2[i] <- sample(group, 1)
  }
  group <- group[-match(df$feedback2[i], group)]
}

I do not get an error message, but df$feedback2 just stays the same for all students:

 group name feedback1 feedback2
1      1    a         2         1
2      1    b         3         1
3      1    c         3         1
4      2    d         1         2
5      2    e         1         2
6      3    f         4         3
7      3    g         4         3
8      3    h         2         3
9      4    i         1         4
10     4    j         3         4
  1. What is the mistake in my second loop for feedback2?
  2. What would be (in general) a better way to do approach the problem?

Best regards




Issue of random background in firefox browser

I've got script of random background which is working of every browser except firefox (only mobile version). In this browser the images not covering all screen but some part.

Here is a code:

<script>function pic() {

var bgm = ['image1.webp', 'image2.webp'];

$('body').css({ 'background': 'url(' + bgm[Math.floor(Math.random() * bgm.length)] + ') no-repeat', 'background-attachment': 'fixed', 'background-position': '50% 50%', 'background-size': 'cover' }); }

pic();

Why on firefox browser (only mobile version) the image not cover all screen?




dimanche 17 décembre 2023

How to predict Random numbers

If I have some code like this:

import random
for i in range(10):
    print(randint(0,1))

Now I know the previous 10 outputs how can I predict the 11th one? I know it's possible, how can I find it?




lundi 11 décembre 2023

Excel VBA Code for lottery and duplicates

I want to make a lottery in Excel where I can draw aprox 30 random numbers between 1-1000 and where the 30 numbers aren't duplicates.

I can't figure out how to check if the Array have duplicates and run the code again if so.

I currently use below VBA code to generate random numbers/winners. I do not think this is the most efficient way but this is what i got... :)

Public Sub CommandButton1_Click()
    Dim MIN, MAX, OUT, i
    Static a, n, z
    MIN = Array(1, 1, 1, 1): MAX = Array(10, 10, 10, 10): OUT = Array("Q1", "Q2", "Q3", "Q4")
    z = UBound(MIN)
    If Not IsArray(n) Then ReDim a(z): ReDim n(z)
    For i = 0 To z
        If n(i) = 0 Then Reset a(i), n(i), MIN(i), MAX(i)
        Range(OUT(i)) = a(i)(n(i)): n(i) = n(i) - 1
    Next
End Sub

Private Sub Reset(a, n, MIN, MAX)
    Dim i, j
    Randomize: n = MAX - MIN + 1: ReDim a(1 To n)
    For i = 1 To n
        j = Rnd * (i - 1) + 1: a(i) = a(j): a(j) = i - 1 + MIN
    Next
End Sub

If you can help me or know of a better way to code this please hmu! :)

Thanks! Oskar