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




mercredi 6 décembre 2023

Difficulty generating unique random numbers using Python's random module [closed]

I am working on a project where I need to generate a list of unique random numbers in Python using the random module. I've tried using random.sample to achieve this, but I'm running into issues with [describe the specific problem or error].

Here's a simplified version of my code:

import random

# Some code here to initialize a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Attempt to generate a random subset of unique numbers
try:
    random_subset = random.sample(numbers, k=5)
    print(random_subset)
except ValueError as e:
    print(f"Error: {e}")

Can someone please guide me on how to properly generate a subset of unique random numbers using the random module in Python?




mardi 5 décembre 2023

Equal group randomization in groups already assigned treatment

I have a list of 80 subjects already randomized into 4 groups (1-4). Now, I want to introduce a crossover design so each subject will eventually be in all 4 groups. Tried to do this with the sample function, but I need an equal distribution of each possibility of the 3 remaining groups to be randomized.

For example: Subject A was already randomized to group 1. Now I need to randomize the remaining 3 groups (2, 3, 4) There are 6 options to randomize the 3 remaining groups: 234 243 324 342 423 432

So I need to ensure that every subject who was initially assigned to group 1 (20 subjects) gets an even distribution of one of those 6 permutations above. The same for every subject initially assigned to group 2, 3 and 4

I tried

#for loop going through each row of the dataframe
for(x in 1:nrow(crossover)) {
  #TRIAL 2
  #randomly selects an integer between 1 and 4

 y<- sample(1:4, 1, replace = TRUE)
  
  #if it equals Assigned.Arm.Index, then randomly select again
 while (y == crossover$Assigned.Arm.Index[x]){

    y <- sample(1:4, 1, replace = FALSE)
  }
  
  #set New.Arm.Index column to y
  crossover$Trial_2[x] <- y

crossover is the name of my dataframe, assigned.arm.index is the premade initial randomization. I created 3 new columns for the remaining randomization I'm trying to fill.

When i asked for help regarding equal randomization across all subjects, I was told to use the rep function, something like:

sample(rep(1:6, 4) , rep= FALSE) 

but it's not fully making sense to me. Im new to R and appreciate any help!




Finding a sum of random numbers in Excel

I have a column of 57 purchases and a column of 18 payments. Is there a way to look up which purchases total a certain amount? Example would be if I had purchases of $10, $15, and $1 in column A, and one of the payments was $26 in column B, it would be identified?

I've tried pivot tables and a variety of formulas and nothing has worked.




Running simulations on high performing computing cluster generates the same results

I am performing Gillespie simulations a thousand times which take a very long time. I decided to run 10 scripts each with 100 simulations on high performing computing clusters of my school. However, once I did this and checked the results, the list of the results from the first script are the same as the second script. Meaning if I run the same scripts twice I get the exact same list of results back. Why is this happening given the fact that Gillespie simulations generate random results?




lundi 4 décembre 2023

Dummy data: generating random text and numerical data into one csv/excel file?

So I'm trying to generate dummy data that contains 3 columns: sq. feet, price and Borough. For the first two, which are purely numerical this is fine. I have 50,000 rows of data for both on a spreadsheet. However, when I add Borough and specify random values from a list I receive the following output:

       Sq. feet    Price  Borough
0           112   345382        5
1           310   901500        5
2           215   661033        5
3           147  1038431        5
4           212   296497        5

I have not used a package associated with numerical generation like np.random.randint

Instead I used "Borough" : random.randrange(len(word))

Where have I gone wrong?

My code below

import random

import pandas as pd
import numpy as np

WORDS = ["Chelsea", "Kensington", "Westminster", "Pimlico", "Bank", "Holborn", "Camden", "Islington", "Angel", "Battersea", "Knightsbridge", "Bermondsey", "Newham"]
word = random.choice(WORDS)
np.random.seed(1)
data3 = pd.DataFrame({"Sq. feet" : np.random.randint(low=75, high=325, size=50000),
                     "Price" : np.random.randint(low=200000, high=1250000, size=50000),
                      "Borough" : random.randrange(len(word))
                     })

df = pd.DataFrame(data3)
df.to_csv("/Users/thomasmcnally/PycharmProjects/real_estate_dummy_date/realestate.csv", index=False)

print(df)

I'm expecting a random line of word values from the WORDS [], instead the return value is just the number 5. It's obviously meaningless making another module just for the text-based data and printing them in different files.