mercredi 10 janvier 2024

Int won't print in the txt file [closed]

I am trying to print a random generated serial along with a name and a date in a txt file. The name and the date prints and get saved but only a question mark appears in the place of the supposed serial digits.

I tried changing the int to long but it does nothing, so does the suggestion.




vendredi 5 janvier 2024

Random.Next() generates same value forever. in Paralled.Foreachasync [duplicate]

I create a Random object at the start of my method. Then in that method I run a Parallel.Foreachasync loop.

Inside this I generate 2 random numbers using Random.Next(). I then check to see if the numbers are the different in a while loop. In the while loop I generate a new random number until they are different. For the first few thousand loops this works fine. Then suddenly it gets stuck generating the same number over and over again...

var room1Index = rnd.Next(0, slot1.RoomList.Count - 1);
var room2Index = rnd.Next(0, slot2.RoomList.Count - 1);

while (room1Index == room2Index)
{
    room2Index = rnd.Next(0, slot2.RoomList.Count - 1);
}

I have checked the roomlist count and it is 4 so I would expect the two numbers to be different at least after a few iterations... However it always generates 0

Why could this be happening?




jeudi 4 janvier 2024

Generating a random sequence of numbers in Python using a dash [closed]

It is necessary to make a random sequence of numbers in Python through a dash, for example, 46417635-7485-8418-8754-651578178453.

Using the random module and the randint function, it was possible to generate numbers in a certain interval (str(random.randint(100, 150). How to make a sequence of numbers separated by a hyphen?




How to give equally probable chances for random loot box opening? [duplicate]

Let's say I am working on a game in C# and want to give the player ability to open a loot box. Let's say we have 5 tiers of rarity for all items in the game. Common - 60%, Uncommon- 20%, Epic - 7.5%, Special - 7.5% Legendary - 5%

Most code I see online says to generate a random number between 1 and 100 for example, and then compare that random number to each rarity until you get a hit.

However, how could this be possible of "Epic" and "Special" both have the same probability? Let's say the random number is 6. How do I know if I should give them a special or an Epic? What I really want is there to be a 7.5% chance that epic and also a 7.5% chance that 'special' can be obtained. How do I do that?




mercredi 3 janvier 2024

Given a RNG in python, Estimate Pi [closed]

I recently watched a popular youtube video that posed the question:

Given a random number in [0,1], write a python program that estimates pi

Before the video gave their solution I came up with the following:

import random 
import math

a=0

n = int(input("Please specificy a number: \n"))

for i in range(n):
    x_i = random.random()
    y_i = random.random()
    d_i = math.sqrt((x_i)**2 + (y_i)**2)
    if d_i <= 1:
        a=a+1


print(4*(a/n))

I watched the rest of the video and my answer was logically equivalent with just some minor differences in syntax. My background is in mathematics so I was able to come up with the mathematical solution that yielded some pseudocode, but I am a very novice programmer so then I googled my way through turning the pseudocode into functional python code. My question for you! experienced users and programmers is: can you tell me a logically equivalent solution to mine that would be preferred if you saw it at work?

Now, I know that is a subjective question that could depend on situation, style, and preference, so let me try to make this question more fit for the community:

Please re-write the above program and point out why yours has superior:

  • elegance
  • readability
  • speed
  • utilization of improvements in python language
  • robustness



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.