mardi 24 novembre 2020

how to storing pre-defined random numbers in c++ [closed]

randNum() function internally contains a sequence of pre-defined 10 numbers E.g.) 3,7,2,5,1,2,8,9,4,6

Then the function returns one number a time sequentially. In the above case, the function should return 3 for the first call and 7 next, and so on. The sequence must be rotational. If the function is called 12 times, the value returned must be 7

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<random>

int randNum();
using namespace std;

int main()
{
    int num;
    cout << "Enter the number ";
    cin >> num;

    int rNum[100];

    for (int i = 0; i < num; i++)
    {
        rNum[i] = randNum();
        cout << rNum[i];
    }
}

int randNum()
{
    static default_random_engine engine(static_cast<unsigned int>(time(nullptr)));
    uniform_int_distribution<unsigned int> randomNum(0, 9);

    static int rNum[10];
    int *ptr;

    for (int i = 0; i < 10; i++)
    {
        rNum[i] = randomNum(engine);
        ptr = &rNum[i];
    }
    return *ptr;
}

But this code doesn't contain pre-defined 10 numbers. I don't know how to fix it.




TypeError: can only concatenate str (not "function") to str .random str in in python telegram bot. i tried it into str(nature) but didnt work

def nature():
    outputs = [
        "Adamant", "Bashful", "Bold", "Brave", "Calm", "Careful", "Docile", "Gentle", "Hardy", "Hasty", "Impish", "Jolly", "Lax", "Lonely", "Mild", "Modest", "Naive", "Naughty", "Quiet", "Quirky", "Rash", "Relaxed", "Sassy", "Serious"]
    nature = random.choice(outputs)
    return 

def one(update: Update, context: CallbackContext) -> None:
    """Show new choice of buttons"""
    query = update.callback_query
    query.answer()
    keyboard = [
        [
            InlineKeyboardButton("Stats", callback_data=str(TWO)),
            InlineKeyboardButton("Move set", callback_data=str(THREE)),
            InlineKeyboardButton("IVs/EVs", callback_data=str(FOUR)),
            
        ]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    query.edit_message_caption(
        caption="<b>Lv.</b> 56 | <b>Nature:</b>" + nature +"\n<b>Types:</b> [Search Internet noob]                                                        \n<b>Exp.</b> 1,250,000", reply_markup=reply_markup, parse_mode=telegram.ParseMode.HTML
    )
    return FIRST



PHP random number update (loto)

Good day everyone,

First I want to apologize but I couldnt find any solution or question on stackoverflow. Im not php coder but I need to make random namber generator on localhost, which I made easily by mt_rand function, it is in loop where it shows every 0.1 sec another rand number with usleep (it goes 50 times), but it is generating numbers next to each other.

What I want is, it should change only one number on screen, so there will be in the middle of the screen one number randomized and every 0.1 sec changing so it will look like random loto. So is there a function that can update or clear old number and show in its place another for every iteration ?

And of course I want to stay with easiest possible code. So my old code for random number is here:

for( $i = 0 ; $i < 50 ; $i++ )
        {
          echo mt_rand(0, 30) . ' ';
          flush();
          ob_flush();
          usleep(100000);
        }



Linked List Round robin game Java

I am trying to do a classic round robin game which each of the player from 1-5 would get a random number but for each round that random number would add another random number and when the number reaches 11+ points it will show winner.

Here's what I've got so far. import java.util.Collections; import java.util.LinkedList;

public class RoundRobin {

public static void main(String[] args){
    LinkedList<String> list = new LinkedList<String>();
        int max = 11;
        int min = 1;
        int range = max - min + 1;
    
    System.out.println("Round 1");
    list.add("Player 1: ");
    list.add("Player 2: ");
    list.add("Player 3: ");
    list.add("Player 4: ");
    list.add("Player 5: ");
    
    for(int i=0; i < list.size(); i++)
        {
          int rand = (int)(Math.random() * range) + min;
          System.out.println(list.get(i) + rand);
        } 
    System.out.println("");
    System.out.println("Round 2");
    for(int i=0; i < list.size(); i++)
    {
      int rand = (int)(Math.random() * range) + min;
      System.out.println(list.get(i) + rand);
    }
}
}

and the output goes like this (In the second round the value must be change to what was added in random):

Round 1
Player 1: 9
Player 2: 8
Player 3: 1
Player 4: 11
Player 5: 9

Round 2
Player 1: 3
Player 2: 6
Player 3: 5
Player 4: 10
Player 5: 9

The sample output is something like this:

Round 1
Player 1: 4
Player 2: 8
Player 3: 3
Player 4: 2
Player 5: 6

Round 2
Player 1: 7 //It randomly added 3
Player 2: 10 //randomly added 2
Player 3: 8 
Player 4: 7
Player 5: 9



Seem failure to generate random numbers

I have inspect about generates of random number. You can see that random numbers is generated four times in the nested loop:

prob_burn = 0.1
for times in range(20):
        
    for i in range(1, row-1):
        for j in range(1, col-1):

            if yards[i][j] == 2:
                rand = np.random.rand() 
                if yards[i+1][j] == 1: # South
                    if rand < prob_burn:
                        yards[i+1][j] = 2
                    else:
                        yards[i+1][j] = 1

                rand = np.random.rand()        
                if yards[i][j+1] == 1: # East
                    if rand < prob_burn:
                        yards[i][j+1] = 2 
                    else:
                        yards[i][j+1] = 1 

                rand = np.random.rand()        
                if yards[i-1][j] == 1: # North
                    if rand < prob_burn:
                        yards[i-1][j] = 2
                    else:
                        yards[i-1][j] = 1

                rand = np.random.rand()
                if yards[i][j-1] == 1: # West
                    if rand < prob_burn:
                        yards[i][j-1] = 2 
                    else:
                        yards[i][j-1] = 1

                yards[i][j] = 0
            else:
                continue
    print(rand)

when the 'yards' multidimensional array code is:

row = 42
col = row

yards = [[0 for i in range(col)] for j in range(row)]
prob_tree = 0.8 

for i in range(1, row-1):
    for j in range(1, col-1):
        rand = np.random.rand()
        if rand < prob_tree:
            yards[i][j] = 1
        else:
            yards[i][j] = 0

yards[6][6] = 2

I have been confused when compiled this. Its gives the output:

0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566

That's seem not really generating random of numbers between [0,1]. I have already tried to growed that 'times' loop more than 20 and 'prob_burn' values, but still gives a similar characteristic. How to solve it?




lundi 23 novembre 2020

Select random cell in range

I'm trying to perform an action in VBA on a range of cells. I would like the selection of the cells to be random not in the order of how the range is setup.

Sub Solver_Step_Evo()
    Set Rng = GetRange(ThisWorkbook.ActiveSheet.Range("Variable_Range"))
    For Each i In Rng
       'perform an action on I where I is randomly selected.
    Next i
End Sub

My preference is it randomizes the order not just randomly select a cell where a cell can be picked more than once.

Thanks in advance.




how to get a random String from a String array

So basically I'm doing a Movie plan maker and I have to set in a text box a couple movies and then press a button to add the movie to the array, then I have to do the same thing with a snack, at the end I have to tipe the name of the movie and when I press a button for show me the movie it has to show me the movie that I type in and get a random snack, my issue is I don't know how to get a random snack, also my both arrays (movie and snack) has to have 50 spaces in the array and I does not matter if the snack repeat.

package com.example.movienightplanneracastillo;

import java.util.Random;

public class MovieTheater {

    Random random = new Random();

    private String[] Movies = new String[50];
    private String[] Snack = new String[50];
    private int nextEmptyIndex = 0;

    public boolean addMovie(String movie) {
        if (nextEmptyIndex < Movies.length) {
            Movies[nextEmptyIndex] = movie;
            nextEmptyIndex++;
            return true;
        } else {
            return false;
        }
    }

    public boolean addSnack(String snack) {
        if (nextEmptyIndex < Snack.length) {
            Snack[nextEmptyIndex] = snack;
            return true;
        } else {
            return false;
        }
    }

    public String findAllMatches(String name) {
        String namesFound = "";

        for (int i = 0; i < nextEmptyIndex; i++) {
            if (Movies[i].toLowerCase().contains(name.toLowerCase())) {
                namesFound = namesFound + Movies;
            }
        }

        return namesFound;
    }

    public String getMovie(String movieName) {
        int Index = -1;
        for(int i=0;i<Movies.length;i++) {
            if (Movies[i].equals(movieName));
            Index = i;
            break;
        }
        if(Index == -1) {
            return "";
        }
        else {
            return Movies[Index];
        }
    }
}

this is the code for my MovieTheater class if it helps in something.