This tag is for questions pertaining to random numbers, whether pseudo random or truly random.
5
votes
2answers
38 views
Weighted probabilistic sampling
Problem description.
Returns strings with probability determined by the frequency
of each of the strings.
eg: if "foo" has weight of 50% and "bar" has weight of another 50% then both foo and
bar ...
5
votes
3answers
69 views
Could random unique selection using while cause an infinite loop?
I recently came across code that looked something like this (generates 4 random numbers in an array, this is not the actual code, I just wrote this up now, so it's untested):
var uniqueNumbers = new ...
17
votes
6answers
3k views
Guessing a unique 4 random digits number
I've created a simple game, in which the user needs to guess 4 digits number between 0-9, generated randomly using Random() Each of the 4 digits are different from each other, with no repeated digits. ...
21
votes
9answers
4k views
Generating Even Random Numbers
I have this class which is used as part of a game. It needs to generate Random Even values, which is done by generating random numbers until the result is even.
Is there a better way to do this?
...
6
votes
6answers
744 views
Random number game
I have created a small 'game' which basically asks the user for the row number and column number and then picks a random number and if that number is above 11, out of 15, the user wins. When the user ...
5
votes
2answers
129 views
Generating random numbers
I'm trying to refactor the following code and could use some suggestions. Basically, three functions generating random numbers in different ways are called:
import java.util.Random;
public final ...
6
votes
2answers
101 views
Generating random HTML color grid
This is a Python script that generates a grid of random HTML colors. It works by reading random data from /dev/urandom and then encoding it as a hexadecimal string. This string is spit into ...
2
votes
2answers
93 views
rand number generator with limits in ruby
I have an array of 1000 numbers, randomly generated. 10 of those numbers must be between 0-9 (including 0 and 9), and 10 of those numbers must be between 991 and 1000 (including 991 and 1000). This is ...
4
votes
1answer
111 views
Function to produce unique random numbers
I've been learning Haskell for a few weeks after coming from a C# background. I've written a function to return a number of unique Ints from a specified range:
module MrKWatkins.Random (
...
5
votes
4answers
118 views
Valid and safe use of SpinLock in Singleton?
Is this a valid and safe use of .NET's System.Threading.SpinLock?
Why am I doing this?
Random's public methods are not thread-safe. I could be calling from any thread which I do not know ...
4
votes
1answer
59 views
is there a more functionally idiomatic way of generating valid dates in f#?
open System
type Date = System.DateTime
type SafeDate =
| WorkingDate of Date
| InvalidDate of Exception
let dateAttempt (x: int, y:int, z:int) =
try
let returnDate = ...
-3
votes
1answer
84 views
trivia questions with 10 random questions from 24 but without repeating the questions
I have a big problem with my project 4-5, I'm trying to have random 10 questions of 25 and not repeating the same questions but I really don't know how to do it. Do you have any hints for me? this is ...
8
votes
2answers
280 views
Guess a random number between a selected interval
My project for my class is to create a Java-based game where the user must enter a number between 1-20 and a number between 250 and 300. The computer randomly chooses a number between those 2 numbers. ...
12
votes
5answers
510 views
Optimize random number generator
I have a method that generates a 10 digit random number where each digit occurs only once.
eg:
0123456789
3645720918
Is there any way this method could be optimized/improved?
private static ...
2
votes
3answers
113 views
Modeling a pair of dice using composition in C++. Am I doing it right?
I've written a program all by myself, but I just want to make sure I did it right and if anybody has any suggestions on improving it in any way.
Define and implement the Die class depicted by the ...
2
votes
1answer
167 views
How to improve efficiency of my roulette game?
Well, I'm kind of new to C++ and was just wondering if anyone could give me tips on how I could make my code more efficient (I added some comments to the code to help you understand it better).
Is it ...
4
votes
4answers
591 views
Random walk on a 2D grid
The program assignment:
A drunkard in a grid of streets randomly picks one of four directions
and stumbles to the next intersection, then again randomly picks one
of four directions, and so ...
1
vote
2answers
68 views
How to make this random number game better?
I'm a newbie trying to teach myself Python. Could somebody tell me if there is a better way to write this? I am also curious as to whether or not there is anything I should not do. This code does ...
0
votes
1answer
84 views
Random string generator [closed]
I have made a simple number generator, and I have a question: is it possible for the generator to eject "red", "blue", "green", " yellow" and "white" instead of the numbers 1-5?
namespace ...
1
vote
2answers
587 views
A simple Dice Roll game
This is the code for a simple dice roll. Any opinions for this are welcome and any suggestions to improve it would be appreciated. Is there a more efficient algorithm to generate random numbers in ...
1
vote
2answers
214 views
Review of Dice class
I don't intend on implementing this into an application yet as I'm going to test it first. As such, it doesn't do much beyond rolling. It also overloads some "typical" operators such as == and !=.
...
5
votes
3answers
834 views
Review of craps game rules and code
I'm developing a small craps game in C++, and my C++ skills are a bit rusty. I really need someone to review my code to ensure that I have the rules correct.
Game rules:
The player or ...
4
votes
1answer
154 views
What do you think about this uint64 random generator?
I've looked for a good method to generate an integer inside a range of values. I think using modulo is not a good method because the values are not perfectly mapped in the range. So I mapped the ...
3
votes
1answer
344 views
Improve password generator (random)
can you please take a look at my code and improve it (if necessary)? ;)
http://jsfiddle.net/a2r9k/
HTML
<button id ="getnewpassword" title="Click to get a new password.">Get New ...
4
votes
3answers
1k views
Random String generator in C
I created this small function just to practice C code. It's a simple random string generator.
#include <string.h>
#include <time.h>
char *randstring(int length) {
static int ...
1
vote
1answer
166 views
Random integer generation in given range
Can my rand_int function be improved?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int rand_int(int a,int b)
{
//The basic function here is like (rand() % range)+ ...
1
vote
2answers
206 views
Am I on the right track with this random number array?
Is this the best way of doing this? I wanted to make sure the grammar is correct when the array is output.
1 car, 2 cars.
The output is correct but is there an easier way generally? I just want to ...
2
votes
1answer
427 views
Generating random numbers in multiple threads
I'm trying to learn how to use threads to generate lots of random numbers. In particular, I'd like know:
Is the following code thread safe? (I think the histogram class is the only one that requires ...
0
votes
3answers
43 views
RAND() with a generated link
I have a project from my college. I'm making a site which will display different quotations each time the page is loaded. I use the following code:
$con = mysql_connect("localhost","root","");
if ...
-1
votes
1answer
715 views
Count the repeating number from randomly generated integer [closed]
My Code
This is a simple code that generate random integers between 1 to 100.
#include <iostream>
#include <cstdlib>
using namespace std;
int rnd()
{
int iRand = (rand() % 100) + 1;
...
2
votes
1answer
88 views
Opinions/Improvements on a run periodically/timer function in Python 2.7
I'm looking for some opinions on this bit of code that I wrote and if there are any ways it can be improved.
The scripts aim is to run the function runme() every 60 seconds with a random interval ...
2
votes
2answers
127 views
Shuffling algorithm suitable?
Just checking that this shuffling algorithm is suitable. Or maybe there could be improvements?
public static void shuffleArray(Object[] arr, Random rnd){
int lastIndex = arr.length - 1;
for ...
4
votes
0answers
179 views
Random Generation From Sequences
With the inclusion of <random> into C++11, we can finally chuck out std::rand and start using generator with much better properties. Still, it's easy to get things wrong (uniform sampling where ...
1
vote
1answer
463 views
PHP: Random imgur image loader
Just a little thing I made to load 20 random images from imgur. I looked at the way that imgur references images on its site, and I felt like I could probably generate a random string of letters and ...
3
votes
1answer
119 views
Generating random strings
I've created the following string manipulation function for randomizing my passed string in Lua:
require "string"
require "math"
math.randomseed( os.time() )
function string.random( self )
...
0
votes
2answers
238 views
How to take the some elements of a list of random numbers and sort them?
I want to create a file consisting take rows of distinct numbers in ascending order. They are randomly taken from the first total integer. The file will be used to make an excerpt as discussed here.
...
1
vote
4answers
143 views
Can you review my random number wrapper class
I made this class to handle any min max integer value and return a random number in the entered parameter range. I tested it to work with all combinations(I think). But is this totally wrong or ...
3
votes
2answers
294 views
Generating random sequences while keeping sum fixed
The output is a random sequence, and the input is the sum of the sequence.
My solution is generating a random number *rand_num* from (0, sum_seq) at first, then draw another number randomly from (0, ...
1
vote
3answers
159 views
What is wrong with my Genetic Algorithm?
I am trying to understand how genetic algorithms work. As with everything, I learn by attempting to write something on my own;however, my knowledge is very limited and I am not sure if I am doing this ...
3
votes
3answers
820 views
Most efficient way to iterate through arrays/can I use .times method in place of while method?
I created a quick program to test the precision of randomness generated by the .rand method. The primary question is whether I can use the .times method in place of the while code blocks to increase ...
2
votes
1answer
177 views
Discrete random variable generator
There is my SSCCE to generate a value of discrete random variable.
values is set of value the RV can take and procents is equivalent to discrete pdf.
Can you anticipate any issue with this snippet? ...
1
vote
0answers
117 views
Review: C++ Algo - Function
See Review: C++ Algo - Style
I'm looking for a review of the function of this code, answers to the above question which also describes the code use focus on style instead.
Below is the updated code ...
5
votes
2answers
213 views
Review: C++ Algo - Style
Can this code be reviewed?
I've updated this question to be about code style only, as all of its answered focused on this aspect. For the codes function, see Review: C++ Algo - Function
'algo' is an ...
4
votes
3answers
369 views
Extending java.util.Random.nextInt(int) into nextLong(long)
In java.util.Random the Oracle implementation of nextInt(int) is as follows:
public int nextInt(int n) {
if (n <= 0)
throw new IllegalArgumentException("n must be positive");
if ...
10
votes
4answers
416 views
Turning an array of words into a random license plate
This is an assignment question from school:
Write a method called licencePlate that takes an array of objectionable words and returns a random licence plate that does not have any of those words ...
8
votes
1answer
108 views
Creating random maze in Go
/*
Create a random maze
*/
package main
import (
"fmt"
"math/rand"
"time"
)
const (
mazewidth = 15
mazeheight = 15
)
type room struct {
x, y int
}
func (r room) ...
7
votes
4answers
513 views
Randomly Permute Elements in a List
I want to randomly permute a finite list in the most effective and efficient way in C#. My attempt is as follows.
/*===================================*
* Compile it to produce Shuffle.exe *
* ...
0
votes
4answers
299 views
Random “month & day” with JS ( jQuery )
setInterval(function() {
var days_array = [31,29,31,30,31,30,31,31,30,31,30,31];
var m = randNum(12);
var m_limit = days_array[m-1];
var d = randNum(m_limit);
$("code").html("day = ...
1
vote
1answer
195 views
Objective-C CF Random Name Generator
Based on a python name generator grammar I found here I've decided to write my own in objective-C. The idea is I can load the grammar from a plist file and generate random names from it.
I'm looking ...
4
votes
0answers
188 views
Listing the first five perfect numbers in MIPS assembly
I've done some programming in the past, but I'm new to MIPS, which I'm trying to learn on my own. This program lists the first five perfect numbers. It uses the Lucas-Lehmer and Miller-Rabin primality ...