mercredi 5 avril 2023

C++ radnom generator simple solution [duplicate]

In my code I have a lot of classes and I want them to do something, based on random number. I found some solutions, which forces me to make some legacy structures and make a lot of actions to simply get random number. Is there some one-line solution to get random number?

I know about BOOST random, rand() and srand() and std::random_device. I want something less complex




mardi 4 avril 2023

I can create and optimize Bingo in C# Visual Studio? [closed]

I can create and optimize Tombola in C# Visual Studio?

I've already created a code, I commented it in Italian because I'm in Italy and it was a school assignment. is it possible to optimize this code?

What I want to create is a Bingo with two cards with random numbers, but each number has the same chance of coming out (1/90), without having numbers that have a higher or lower chance of being drawn than another number.

link github




lundi 3 avril 2023

Is there a way to get a fixed number of random rows from an SQL table while allowing for repetions

For the example here we will use the table NAMES and I need 3 rows returned.

id name
1 Alpha
2 Beta
3 Chi
4 Delta
5 Epsilon

I know the code SELECT * FROM names ORDER BY RAND() LIMIT 3; will return 3 randomized rows. But it is exclusively three different rows.

Is there a way to in a query get three rows that do not influence each other, and thus allow for repetion? So a result could be Beta, Alpha, Delta. Or Beta, Epsilon, Beta, or even Beta, Beta, Beta.




How to generate random 20 string with random length between 1-100 in assembly language?

My code only generate 10 length but I need to be from 1-100. Pls help

INCLUDE Irvine32.inc
strLen=10

.data
arr BYTE strLen DUP(?)

.code
   main PROC
   call Clrscr
   mov esi, offset arr
   mov ecx, 20
 L1:
   call GenerateRandomString
   Loop L1
   call WaitMsg
   exit
 main ENDP

; string_length = 8
; rand(36**string_length).to_s(36)

 GenerateRandomString PROC USES ecx 
   mov ecx, lengthOf arr
 L2:
   mov eax, 26
   call RandomRange
   add eax, 65
   mov [esi], eax
   call WriteChar ; write character
   loop L2 
   call Crlf
   ret
 GenerateRandomString ENDP

END main



First random number is alway the same

I am trying to write a simple program. I heard in C++ that there are existing functions that are better than rand() (to make random numbers). I am talking about classes from #include <random>. My program goal is to make random numbers.

This is my code:

int randomSpawn()
{    
    unsigned seed = time(NULL);
    std::default_random_engine rng(seed);
    std::uniform_int_distribution<int> d(1, LENGHT);
    
    srand(time(NULL));

    std::cout << d(rng) << std::endl;
    std::cout << rand() % LENGHT << std::endl;

    return rand() % LENGHT;
}

The problem is that the first value is always the same - 17. LENGTH is equal to 30.

So, the oldest version making random number is working - because it displays a random number, but the new version doesn't work because it always displays 17.

I tried to move the seed definition to another part of code, but it didn't help.




Java replicable random number based on parameters

I am trying to create a "map" of random numbers, by only generating numbers within a certain area. The random numbers are used to create decimals which when high enough will create a star in the given location.

Ex:

for(int x = 1; x <= SCREEN_SIZE.width; x++){
        
    for(int y = 1; y <= SCREEN_SIZE.height; y++){
                
        if(star_map.getDouble(x + position[0], y + position[1]) <= (cvg)/100){
                
            stars.add(new Star(x + position[0],y + position[0]));
                
        }
            
    }
        
}

I would like to be able to move the position around, so when it comes back to the same position, it should have the same stars.

I tried setting the seed of a Random based on the x, y, and general seed of the stars, but I kept getting either identical values, or nearly identical (All around 0.71).




Can I randomise the position of a background gradient?

I have this custom CSS code on my Squarespace site that creates a gradient and animates it. I would like to alter this code so that the position of the gradient is randomised every time I reload the page. How would I go about that?

/* Gradient animation */
#collection-631373335b5554367a6d6496, #collection-6315119bb4eaa941a0aa617e, #collection-631518729bf3e53d3de5c632, #collection-63151986700b2b4827241df9, #collection-63151bde5812ae629c05151f, #collection-63151a542c7e4f20c67c6aec, #collection-63151b4e3efb9c1fd9c951f2, #collection-631520e2a99294780df99008, #collection-631521a03ecc7346d794bb07, #collection-63152145f7e82b5663fa3d42, #collection-6315243f7a90bc4192a371f2, #collection-631a6f149c23b71200e5d6ce, #collection-6323c23428c02c6f61ce014a, #collection-63a07f0407c73f305d336cb3, #collection-640761a352606c4720ae27d0, #collection-64298618ce9c9a50c8263f71 {  

  .section-background {
    background: linear-gradient(270deg, #8A90F9, #91F2F2, #92F492, #FFB64D, #FF5C5C, #FC92FC);
    background-size: 10000% 10000%;

    -webkit-animation: AnimationName 60s ease infinite;
    -moz-animation: AnimationName 60s ease infinite;
    animation: AnimationName 60s ease infinite;
}

@-webkit-keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}
@-moz-keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}
@keyframes AnimationName { 
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}

I tried asking ChatGPT. It gave me this code but it did not work

Sure! Here's an updated version of the code that randomizes the initial background position:

/* Gradient animation */
#collection-631373335b5554367a6d6496, #collection-6315119bb4eaa941a0aa617e, #collection-631518729bf3e53d3de5c632, #collection-63151986700b2b4827241df9, #collection-63151bde5812ae629c05151f, #collection-63151a542c7e4f20c67c6aec, #collection-63151b4e3efb9c1fd9c951f2, #collection-631520e2a99294780df99008, #collection-631521a03ecc7346d794bb07, #collection-63152145f7e82b5663fa3d42, #collection-6315243f7a90bc4192a371f2, #collection-631a6f149c23b71200e5d6ce, #collection-6323c23428c02c6f61ce014a, #collection-63a07f0407c73f305d336cb3, #collection-640761a352606c4720ae27d0, #collection-64298618ce9c9a50c8263f71 {

.section-background {
background: linear-gradient(270deg, #8A90F9, #91F2F2, #92F492, #FFB64D, #FF5C5C, #FC92FC);
background-size: 10000% 10000%;
background-position: calc(100% * var(--rand-x)) calc(100% * var(--rand-y));

-webkit-animation: AnimationName 60s ease infinite;
-moz-animation: AnimationName 60s ease infinite;
animation: AnimationName 60s ease infinite;
}

@-webkit-keyframes AnimationName {
0%{background-position: calc(100% * var(--rand-x)) calc(100% * var(--rand-y))}
50%{background-position: calc(100% * var(--rand-x) - 100%) calc(100% * var(--rand-y))}
100%{background-position: calc(100% * var(--rand-x)) calc(100% * var(--rand-y))}
}
@-moz-keyframes AnimationName {
0%{background-position: calc(100% * var(--rand-x)) calc(100% * var(--rand-y))}
50%{background-position: calc(100% * var(--rand-x) - 100%) calc(100% * var(--rand-y))}
100%{background-position: calc(100% * var(--rand-x)) calc(100% * var(--rand-y))}
}
@keyframes AnimationName {
0%{background-position: calc(100% * var(--rand-x)) calc(100% * var(--rand-y))}
50%{background-position: calc(100% * var(--rand-x) - 100%) calc(100% * var(--rand-y))}
100%{background-position: calc(100% * var(--rand-x)) calc(100% * var(--rand-y))}
}
}

/* Randomize background position */
.section-background {
--rand-x: calc( Math.random() );
--rand-y: calc( Math.random() );
}