what's "more" random? Not using random.seed at all, or setting the seed to be current system time's microseconds?
Are there methods to get "better" randomness with the random module?
what's "more" random? Not using random.seed at all, or setting the seed to be current system time's microseconds?
Are there methods to get "better" randomness with the random module?
So I'm trying to make a randomizer that goes from 0 - 8. I have a button that you click in order to get the number. It is an empty paragraph with an id. Here's my code:
HTML:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="tf2generator.html"></script>
</head>
<body>
<p id="changer"> </p>
<button onclick = Randomizer()> Button </button>
</body>
</html>
External JS:
function Randomizer(){
let randomnum = Math.floor(Math.random() * 8);
document.getElementById("changer").innerHTML = randomnum;
}
This literally makes no sense on why it doesn't work, please help.
I have an array like this:
export const QuizArray = [
{
id: 1,
question: 'Which of these factors that contribute to stroke?',
answers: ['High blood cholesterol', 'Smoking', 'Old Age', 'All of the above'],
correct_answer: 'All of the above',
},
{
id: 2,
question: 'Ischaemic Stroke is when blood clots occur in the brain?',
answers: ['TRUE', 'FALSE'],
correct_answer: 'TRUE',
},
{
id: 3,
question: 'Which of the following are possible symptoms of stroke?',
answers: ['Weakness in facial muscles', 'Vision impairment', 'Slurring of speech', 'All of the above'],
correct_answer: 'All of the above',
},
]
How do I randomise/shuffle the id without any repetition? I am using react native, VS Code and Andriod studio.
I want to draw randomly pixels on a screen whereby the probability should not be equally distributed.
Example: Let's say the screen has 1920 x 1080 pixels. On a draw event the probability to be drawn for pixels which lie in a 100 x 100 rectangle at position (500,500) should be 10 times higher then for pixels outside the rectangle.
To achieve this I create first an array which contains the probability. The positions inside the rectangle get a value of 10, all other positions get a value of 1.
for i := 1 to 1920 do
begin
for j := 1 to 1080 do
begin
FProbability[i, j]:=1;
if InRange(i, 500, 600) and InRange(j, 500, 600) then
begin
FProbability[i, j]:=10;
end;
end;
end;
Then I make a list of all pixels:
FPixelList:=TList<TPoint>.Create;
for i := 1 to 1920 do
begin
for j := 1 to 1080 do
begin
for k := 1 to FProbabilty[i, j] do
begin
FPixelList.Add(TPoint.Create(i,j))
end;
end;
end;
The pixel list has now 10 entries for each pixel inside the rectangle and 1 entry for all other pixel positions.
On a draw event I get the pixel position to be drawn by
FPixelList[RandomRange(0, FPixelList.Count-1)]
This works fine.
However I was wondering if there are other solutions for this problem. My solution uses a lot of memory if the screen sizes become bigger and I can only use integer values for the probability.
I have a table of products who have prices, discount values and final prices. Different products can have the same discount values. I want to order by them by the discount value but if 2 of them have the same discount value I want them to be random in the ranking. This way I can show my client top 10 best deals of the products. But for example if there are 15 products and 13 of them have 0 discount value, I want different products to came everytime. Is there a way to do that?
I am trying to make a minesweeper game as a practice project but I have ran into an error when generating the random coordinates for the bombs. Basically, when I run the random.randint() function, it gives me an error when I try to assign it, but not when I print it out.
import random
def assignBombs(grid, k):
m = len(grid) - 1
n = len(grid[1]) - 1
while k > 0:
i = random.randint(0, m)
j = random.randint(0, n)
if grid[i][j].bomb == False:
grid[i][j].bomb == True
n -= 1
return grid
grid = initialize(5, 5)
grid = assignBombs(grid, 4)
The initialize function will make a 5x5 grid of Cells, a class which has the boolean self.bomb. Please let me know if I need to provide this.
Anyway, this will give me the following error when running:
ValueError: empty range for randrange() (0, 0, 0)
However, if I replace the while loop in the function with a simple print statement:
def assignBombs(grid, k):
m = len(grid) - 1
n = len(grid[1]) - 1
print(m, n, random.randint(0, n), random.randint(0, m))
This will print out just fine:
4 4 3 4
Where m and n are the max row and column indices. Any help is appreciated!
So I am making a function in python that simulates dice rolls based on user input. I have 2 versions of the code, one that uses a loop and one that is pure math(which I'm not sure is correct).
# Loop
def simulate_dice_loop(num, sides):
result = 0
while num > 0:
x = random.randint(1, sides)
result += x
num -= 1
return result
# Math
def simulate_dice_math(num, sides):
max = num * sides
result = random.randint(1, max)
return result
I'm highly suspicious if the math one is correct, especially on larger numbers. The loop dice roll tends to stay around half of the max value. However, the math one tends to fluctuate all the time, sometimes even reaching 90% of max-- which is probably highly unlikely on normal occassions.
...Please help on how I could improve the math one because the loop tends to take longer the larger the number-- and also, to satisfy my curiosity on the subject. Thanks ^^