Click here to Skip to main content
15,887,394 members
Everything / Random

Random

random

Great Reads

by Igor Krein
A library of simple extension methods that could be useful for data generation tasks
by Peter Occil
Algorithms to turn biased "coin flips" into biased "coin flips", and how to code them.
by Kornfeld Eliyahu Peter
“Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!” - LEWIS CARROLL
by Peter Occil
Python code for partially-sampled random numbers for accurate arbitrary-precision sampling

Latest Articles

by Alexey Shtykov
The thing that could generate pseudo random numbers faster than standard library does
by Member 4201813
How to calculate transition matrix for linear pseudo-random number generator manipulating its internal state
by Jack Devey
This post delves into the perplexing Monty Hall paradox, examining the probabilities associated with sticking or switching doors in the game scenario.
by ToughDev
How to allow only numeric input in TextBox

All Articles

Sort by Updated

Random 

19 Feb 2024 by Alexey Shtykov
The thing that could generate pseudo random numbers faster than standard library does
21 Sep 2023 by Member 4201813
How to calculate transition matrix for linear pseudo-random number generator manipulating its internal state
13 Jun 2023 by Jack Devey
This post delves into the perplexing Monty Hall paradox, examining the probabilities associated with sticking or switching doors in the game scenario.
7 May 2023 by ToughDev
How to allow only numeric input in TextBox
27 Feb 2023 by gggustafson
Print_Cards to produce PDF file containing specified number of unique Bingo cards
20 Feb 2023 by gggustafson
In this first part, we look at the random arrangement of numbers on a player's Bingo card.
12 Feb 2023 by Divya Ulasala
Examining the differences between java.util.Random, java.security.SecureRandom, and java.util.concurrent.ThreadLocalRandom to generate random numbers
29 Dec 2022 by Peter Occil
Has many ways applications can sample using an underlying (pseudo-)random number generator and includes pseudocode for many of them.
2 Dec 2022 by Rishi 2022
I am doing sample based on the numbers extracted after the filteration operation.Below is the count i am getting:- df_ID[['Bucket','Service']].value_counts() Output:- Eg. Bucket Service Tier 2 das ...
20 Nov 2022 by Manojkumar-dll
I have written a code to generate random number ranging 0.8 - 1.2 while executing the numbers above 1 coming correctly as 1.2,1.22,1.4 etc but in the case of 0.xx its coming like .98,.89,etc I don't need repeating values tried with zeros...
20 Nov 2022 by OriginalGriff
To add to what Greame says, don't do it like that. Declare a single Random instance at class level:private Random random = new Random(); Or: private static Random random = new Random(); and us that instead. The reason why is simple: when you...
20 Nov 2022 by Graeme_Grant
Do you mean that you want a leading zero '0' for numbers less than 1? Then you would need: string ans = val.ToString("0.##"); If you want the same number of decimals, then: string ans = val.ToString("0.##0"); or... string ans =...
9 Nov 2022 by wiam leo
We have images in folders, named (face1.png,...,face6.png) using this code; I can't display images, we randomly retrieve photos: import React from "react"; import jeuxde from "../images/jeuxde.jpg"; export default class JeuDe extends...
9 Nov 2022 by wiam leo
I solved the problems import React from "react"; import jeuxde from "../images/jeuxde.jpg"; export default class JeuDe extends React.Component{ constructor (props){ super(props); this.state= {face: null, compteur: 0, fin:...
7 Nov 2022 by OriginalGriff
The question is, where are the images? If they are on the server, Javascript can't access them because it runs on the Client and has no access to the server filesystem. If they are on the client, where on the client? You are using absolute...
18 Oct 2022 by Manojkumar-dll
Finally found it var result= "0"; if(Desired condition>0) { Random random = new Random(); double val = (random.NextDouble() * (max - min)...
18 Oct 2022 by Manojkumar-dll
I want to generate random float numbers ranging 0.0 to 1.5 and to store it inside a variable the values should store inside the variable under certain condition like in What have you tried? section apology for my poor explanation i tried my...
16 Oct 2022 by Richard MacCutchan
See Random Class (System) | Microsoft Learn[^].
24 May 2022 by CHill60
Look at the line random_char = random.choice(letters) + random.choice(numbers) + random.choice(symbols) For every iteration of the loop you are adding three characters to the array. You could change your loop to be something like for char in...
24 May 2022 by Manbir Singh 2022
I'm writing a code for Random Password generator where user should have an ability to select the length of password from pre-defined range (15,25) and code will generate random password using letters, numbers and symbols. If a user enters a...
24 May 2022 by Richard MacCutchan
You need to select either letters or numbers or symbols each time, not all three. Here is a sample that will do that automatically: import random letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',...
24 May 2022 by OriginalGriff
The way I do it is simple - just use a GUID: uuid — UUID objects according to RFC 4122 — Python 3.10.4 documentation[^] Create it as a Hex string and you get something like this: 11069f3e-bc2b-469c-ab00-2297d1c7e07f Use that as a password and...
16 Mar 2022 by Peter Occil
Python code for partially-sampled random numbers for accurate arbitrary-precision sampling
6 Jan 2022 by innapivov
Please help to write the C code Here is a version of a problem called the random walk. Assume that you`re very tired and dog leaves his lamppost on summer evenings and staggers randomly either two steps in the direction toward home or one step...
6 Jan 2022 by KarstenK
Start with some Learn C tutorial. It wont be to hard if you can already code in some other language, but you need to respect the rules of C like a bunch of braces and declarations. Best is to write some functions and use struct. For some random...
5 Jan 2022 by OriginalGriff
Quote: Well, your advices are good but I can`t lose my scholarship, so if there`s a volunteer to help with a code, + to your karma There are several problems here: 1) You clearly have misunderstood karma. We wouldn't get good karma for doing...
28 Nov 2021 by Grelm
Hello, I'm making a number guessing game in C# for homework. When I run it, if the guess is lower or higher than the random number, it just loops "Guess higher" or guess lower". Any help would be appreciated. using System; namespace...
28 Nov 2021 by Grelm
Figured it out! static void Problem3() { Random random = new Random(); int numNums = 2; int GuessCount = 1; int current = 0; Console.WriteLine("Enter amount of numbers to guess...
2 Sep 2021 by Peter Occil
Algorithms to turn biased "coin flips" into biased "coin flips", and how to code them.
18 Aug 2021 by Richard MacCutchan
This is the same question as How to generate random hexadecimal colors between min and max?[^], and you have already been given the answer.
18 Aug 2021 by Richard Deeming
Generate a random number between 65280 (0x00ff00) and 16711680 (0xff0000). Math.random() - JavaScript | MDN[^] function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min) +...
18 Aug 2021 by Mehrez Kanzari
I'm trying to generate random hexadecimal colors between this range: min= #00FF00 (green) and max= #FF0000 (red). What I have tried: I made my own search but I found only how to generate random hexadecimal colors not between two hexadecimal...
17 Aug 2021 by Maskedllama
Hey everyone.I have made a code that renders 2 different random numbers on screen, but it justs keep updating the numbers. I want the program to stop updating those numbers once the first 2 appear on screen. Here is the code (I posted something...
17 Aug 2021 by Maskedllama
Hey everyone. Thank you for all the advices you gave me. I have been thinking about this issue, and I have rethought what I really want to do. It's my first "big" project, as you can imagine. Anyway, I need to display two random numbers on...
17 Aug 2021 by Patrice T
Quote: I have made a code that renders 2 different random numbers on screen, but it justs keep updating the numbers. I want the program to stop updating those numbers once the first 2 appear on screen. You want to generate the random numbers...
17 Aug 2021 by Richard MacCutchan
The issue is much the same as your previous question at Making this code unfreeze, and then introducing an entry box.[^]. You do not appear to understand how to control your loops properly. I would suggest spending some time working through The...
17 Aug 2021 by OriginalGriff
That is all your start_the_game function does, pretty much - print two random numbers on the screen. And since it is called in a loop that does nothing except repeatedly say "tick at 60 ticks per second" and the call start_the_game that's what...
6 Jul 2021 by Happy kaul
0 I want to remove the single string and want to keep string with minimum length 3 and above i tried to access string with this if(result.string >= 3) but it is giving array length so i tried to access string but i cant. so please anybody who...
6 Jul 2021 by Estys
Replace your return result; withreturn result.filter(x => x.length >= 3); Because the way you generate the combinations, you can't filter short combinations earlier. Cheers
5 Jul 2021 by Greg Utas
I don't know Javascript but have a sense of what your code is doing. You're finding all combinations except for the empty string, which is a good start. But I see nothing in your code that filters out combinations of less than 3 letters. It looks...
15 Jun 2021 by Member 15170612
Hi, I have a question. How do I convert an IF-statement to a WHILE loop without doing an infinite loop? Every time I tried it, I overloaded the computer and had to turn it off.And when I use the IF condition, it doesn't solve my problem at all,...
15 Jun 2021 by Richard MacCutchan
while (failResult == resultNumber && failResult == wrongResult) A variable cannot be equal to two different values at the same time.
15 Jun 2021 by lmoelleb
Learn to debug. It is simple, and the time investment pays of immediately. I understand that it might seem like "I have a lot to learn, so I will put that off and learn it later - for now I just want my program working", but that is a REALLY bad...
13 Jun 2021 by Member 15170612
I am trying to create an application in which I will be offered three answer options (radiobuttons) one will be correct (assigned from the result) but the other two should be randomly generated and different from the correct. It occurred to me to...
10 Jun 2021 by OriginalGriff
Simplest way is to make a generic method which accepts a list and "shuffles" them: place all the questions (correct and incorrect) in a List and pass it to the method. private List Shuffle(List input) { ...
26 May 2021 by Peter Occil
Most apps that use random numbers care about either unpredictability, high quality, or repeatability. This article explains the three kinds of RNGs and gives recommendations on each kind.
23 May 2021 by Member 15170612
Hello, I have a problem with my while-do loop. I'm trying to set my code for counting math games, all math operations are fine except for division. There is a problem with division, such that I would like the division to be residual, so I came up...
23 May 2021 by Richard MacCutchan
You are incrementing secondNumber before you do the division, so the number could already have exceeded firstNumber. Change the loops to straight while: if (firstNumber
23 May 2021 by Patrice T
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes. Random number = new Random(); int maxValue = 10; int total = 0; int firstNumber =...
23 May 2021 by Richard Deeming
Signed integers in .NET are stored using Two's Complement: Signed number representations - Wikipedia[^] If you keep incrementing a positive integer past its maximum value, it will become negative. If you want to throw an exception if that...
20 May 2021 by Member 15170612
Hello, how can I set my WPF Application with random generator number for division without residue? I try to create a math game in which the user chooses the math operation he wants to calculate, then he chooses what level he wants. Addition,...
20 May 2021 by Patrice T
Quote: How can I set division without residue? A division is: dividend / divisor = quotient Draw divisor and quotient, and multiply them to deduce the dividend.
12 May 2021 by Member 15170612
Hello, I would like to ask if there is any simple way to multiply the random generated number by the current millisecond? In my WPF Application I generate two random numbers in quick succession and it happens to me that in most cases they are...
12 May 2021 by Abeer Joshi
Yeah that helped me too, thanks everyone :)
12 May 2021 by OriginalGriff
The solution is simpler: don't use Random like that. Declare a single Random instance at class level (static is fine) and use that for both values: private static Random rand = new Random(); ... int maxprvni = 10; int...
12 May 2021 by OriginalGriff
Repost: Deleted. Please do not repost your question; use the Improve question widget to add information or reformat it, but posting it repeatedly just duplicates work, wastes time, and annoys people. I'll delete this one.
3 May 2021 by CPallini
A very approach is by construction: Randomly generate the second (integer) number, say n2. Randomly generate an integer factor, say f. Let the first number, say n1, be equal to second number multiplied by the factor f (namely n1 = n2 * f)
8 Feb 2021 by Mohammad Shaheen
i want mathematical equation or function to get number between two number .. for example the two number is 5 and 10 .. when i send 5 will make process and returned number for example 7 this result will not be repeated .. and when i send 8 . will...
8 Feb 2021 by Member 15067916
I searched and searched the internet and did not like the solutions I was seeing. Yes, I saw the Fisher-Yates Shuffle but its like greek to me. I wanted a simpler, easy to follow solution that was more how it would happen in the real world (ie....
30 Jan 2021 by Anuchen
I'm trying to develop a Java program which presents Users with encrypted word and asks them to decode it. Currently I present random words from an array after encrypting them with a random Caesar shift number. Could you please advise on how this...
30 Jan 2021 by Patrice T
Advice: do not use the same random as a mean to set the key to Cesar encoding and as a mean to choose the word in list. String[] plainTextWords={"and","now","mean","hey"}; int shift = (int)(Math.random()*10); System.out.println(shift);...
19 Jan 2021 by Member 13987161
i'am trying to generate a random password using python random library where i've defined a number of alphabets(upper, lower & digit) in a variable and also defined the password length size should be 3(EXAMPLE), now using if statement i tried to check whether the random generated password would...
7 Jan 2021 by GWLBSA
I am bored, and of course, when you're bored and know how to code, you just code whatever you can think of. I thought of making a die-rolling application, and the user can choose to roll anywhere from a 1 sided die to a 50 sided die. I tried out...
7 Jan 2021 by CPallini
You are using the octal format specifier %o while you should use the decimal one %d. For example, the (decimal) number 50 becomes by 62 = 6*8+2 in octal representation. By the way, you know, you could write your program in a more compact way:...
29 Dec 2020 by Peter Occil
Some of the most common random generation questions based on a Q&A site question analysis.
20 Nov 2020 by Member 14969271
I am a VB.NET beginner. I had faced the issues on loading string array randomly and add into datagridview. For the project, first of all, I will import the data from excel to datagridview. After that, I need to load the string array randomly and...
20 Nov 2020 by Richard MacCutchan
Dim rnd As New Random() Dim ARR As String() = {"abc", "cde", "efg", "hij"} For Each value In ARR.OrderBy(Function(x) Rnd.Next()) DataGridView1.Rows(1).Cells(1).Value = value # this only writes in row 1 Next Change the row value to an...
5 May 2020 by Member 4201813
Jumping along the System.Random output sequence with logarithmic complexity
21 Mar 2020 by OriginalGriff
Random numbers are just that: random. And that means that the next number returned by a generator does not rely on previous results at all: it is as likely to generate any number in the sequence each time you try to get a new random number. And...
21 Mar 2020 by Patrice T
Quote: i want mathematical equation or function to get number between two number Short answer is "it does not exist". Both requests are mutually exclusive. The closest thing is a "shuffle" algorithm, just like you shuffle a deck of playing cards...
28 Dec 2019 by Member 14549747
I'm working on a java project where I have a method that returns a hash function as a pair of integers a , b that exist in {1 , 2 , ... , 10000} and are picked randomly . To pick the random numbers I have an "rnd" variable . My problem is that I have learned that prime numbers are better for ...
28 Dec 2019 by OriginalGriff
You don't use random values to generate a hash - the idea of a hash is that it is a smaller, simpler, easier-to-manipulate version of the original input. And that requires that the same input always gives the same hash value, which use of random numbers does not allow. Think about it: if a hash...
29 Nov 2019 by RickHamton
I want to create an alert effect on a button by cycling between two colors randomly. What I have tried: private void timer1_Tick(object sender, EventArgs e) { Random rand = new Random(); int A = rand.Next(256); int R = rand.Next(256); ...
29 Nov 2019 by RickHamton
int timeleft = 2; private void timer1_Tick(object sender, EventArgs e) { Color color1 = Color.Red; Color color2 = Color.WhiteSmoke; if (timeleft > 0) { NotificationBtn.BackColor = color1; timeleft...
21 Oct 2019 by Member 12696135
OK, Just throwing this out there... EASY PRIME GENERATION IN C#.NET I think this approach will be far more useful to most readers than some of the suggestions above. Forgive me, but after reading this thread I figured that this little trick is sorely missing from the discussion. I don't...
21 Oct 2019 by HitsugayaHisagi
Would like to ask, how do I generate 2 random numbers and then check if it is a prime? I would like to generate both and check them all in one click. I tried to write the code in C# but there are some invalid arguments. Here is my code.protected void btnstart_Click(object sender, EventArgs...
8 May 2019 by Maciej Los
Quote: You can use this syntax: if myItem in list: # do something Also, inverse operator: if myItem not in list: # do something It's work fine for lists, tuples, sets and dicts (check keys). Note that this is an O(n) operation in lists and tuples, but an O(1) operation in sets...
8 May 2019 by Dechen Wangmo
I get new current list randomly but the randomly generated list should have TRUE in it, eg.['TRUE','FALSE' , 'FALSE', 'FALSE',] or ['FALSE' , 'FALSE', 'FALSE','TRUE'] import random list2 = ['TRUE','FALSE' , 'FALSE', 'FALSE', 'FALSE','FALSE','FALSE' ] current = [] def randompicker(): for...
11 Apr 2019 by Gerry Schmitz
You should shuffle list2 in the "outer loop" (not before), if you want "combinations within combinations".
11 Apr 2019 by Member 14035055
I have two lists with 4 strings each. I shuffle them and then I select a string on each list to create a combination of strings from both lists. I want the four combinations (written below) repeated four times (so 16 combinations in total) but each of these combination should be presented in a...
3 Feb 2019 by Maciej Los
As far as i know OBS software is used for screen recording... So, all you need to do is to find software, which can: - convert images (ading effects), - display images from source folder in full-screen mode, - create slide show (in a specific amount of time) - etc. I'd recommend to use...
3 Feb 2019 by Member 14138269
Hello everyone: First things first, I'm not a coder or something like that but I need someone that can help me... I have been looking all over google, youtube and internet for a program that can be able to basically do 3 things (and haven't found one yet so that's why I'm here asking for...
2 Feb 2019 by OriginalGriff
I don't think you are ever going to find exactly what you want, so that means a custom solution - writing the code, basically. And if you aren't a coder, then you will have to find a coder to do it for you - but this is not the place for that, we do not arrange commissions here. I suggest you...
1 Oct 2018 by Richard MacCutchan
You have lots of #include statements that do nothing. You have lots of cout > y (and other variable names), that also do nothing. I suggest you go back to your documentation and read up on the cin method, passing parameters to functions, including header files, etc., etc.
1 Oct 2018 by Member 14003985
I did not include any random functions and it still returns a random number at the end of the program. Please help. This is a math calculator that can do basic math operations. #include #include #include #include #include #include #include #include ...
1 Oct 2018 by OriginalGriff
That code won't compile: the function you call several times playagain isn't there, nor is askMathFunction If your code doesn't compile cleanly, then it doesn't produce an EXE file, and you can't execute it. So the version of code you are testing is not the same as the code you are looking at,...
25 Sep 2018 by YouCanHateMeNow
Hey guysHow can I create a random Password Generator?Module Module1 Sub Main() Dim passwordlength As String Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?!" 'All characters which will be used for the random passowrd ...
21 Sep 2018 by Patrice T
Quote: I try to make oracle which predicts next random number in a sequence. that is doomed to fail. Nicolas Cage is the only one able to do this (see movie Next).
21 Sep 2018 by Member 13993058
I try to make oracle which predicts next random number in a sequence. I have an array of random generated numbers. What I have tried: I know that i should do it linear way.
21 Sep 2018 by OriginalGriff
Depending on the range of values for your random number sequence, you are probably on a loser - rand doesn't just have an algorithm, it also has a seed value which is normally initialized from the system clock at some point in the application. Depending on the seed, where the algorithm "starts"...
17 Sep 2018 by Richard MacCutchan
counter = 0 while 1: passwd = ''.join(random.sample(sbc,passlen)) if passwd == 'abc': print('password matched') break print(passwd, "does not match") counter += 1 print(counter, "iterations")
8 Sep 2018 by Member NFOC
Using the C++ random number generator rand(), implement a biased coin that outputs 1 with probability p and 0 with probability 1–p. Do not seed the random number generator within the function you will be implementing! What I have tried: I have tried this to generate 0 and 1: float...
8 Sep 2018 by Patrice T
1) Choose your language. 2) study how rand is working in that language. Quote: implement a biased coin that outputs 1 with probability p and 0 with probability 1–p. the important word is 'biased'. This imply a 2 step drawing. Say the odds are 0.33 and 0.67. - draw a random number - check the...
8 Sep 2018 by OriginalGriff
Read the question carefully: it requires you to produce a BIASED coin: one that returns more 0's than 1's or vice versa, dependent on the probability given in p So, instead of using 2 which gives you a straight 50:50, you need to use the value of p to bias your results. Since p is a floating...
27 May 2018 by CPallini
Try #include /// need this for srand() -- for random numbers #include /// need this for time() -- time #include /// need this for cout> using namespace std; /// need this for cout> int main() { srand(time(0)); cout
27 May 2018 by Member 13843522
I am almost finished with this program, other than I cannot figure out how to get a new set of random numbers when the user chooses to re-roll (rolling R when prompted the first time). Below is my code -- can anyone help me find out where I went wrong with syntax, seed, etc...? What I have...
27 May 2018 by Patrice T
Quote: I am almost finished with this program, other than I cannot figure out how to get a new set of random numbers when the user chooses to re-roll (rolling R when prompted the first time) You need to workout the logic, a simple method is take output and note what is going on behind the hood....
27 May 2018 by Richard MacCutchan
Create a function that does the rolling, and allow it to accept a parameter which tells it whether to seed the randomiser or not: int DiceRoll(bool seed = false) { if (seed) { // seed the randomiser srand((int)time(0)); } int roll = 1+rand()%6; // get a random...
14 Apr 2018 by OriginalGriff
Quote: the answer was writtin in python i don't know python. Then you will either have to learn Python, or keep looking: we are not a "code conversion service".
14 Apr 2018 by dolfijn3000
so i'm working with perlin noise in c#. after experimenting with it i wanted to make a seammless perlin noise map. i want a map where the above edge of the map can continue in the bottom edge. is there someone that can help me with this ? What I have tried: searching the internet the only...