random

Java: Can (new Random()).nextInt(5) always return the same number?

Sometimes this piece of code always returns the same number (and sometimes it works fine): (new Random()).nextInt(5) I have suspicions where the problem is - it probably always creates a new Random with the same seed. So what would be the best solution: create a static var for Random() and use it instead. use Math.random() * 5 (loo...

iPhone SDK: Setting up an animation to create a blinking effect.

In an iPhone application I'm developing I have a Image View that displays eyes and I want to make them blink, an obviously blinking is a random thing, it can't be routine and keep repeating like a UIImageView Animation would do on repeat. How would I set up an animation that displays one frame (blinked eyes) then sets back to the origi...

What is a good random number generator for a game?

What is a good random number generator to use for a game in C++? My considerations are: Lots of random numbers are needed, so speed is good. Players will always complain about random numbers, but I'd like to be able to point them to a reference that explains that I really did my job. Since this is a commercial project which I don't h...

How do I speed this up?

The following code makes a list of names and 'numbers' and gives each person a random age between 15 and 90. #!/bin/sh file=$1 n=$2 # if number is zero exit if [ "$n" -eq "0" ] then exit 0 fi echo "Generating list of $n people." for i in `seq 1 $n`; do let "NUM=($RANDOM%75)+15" echo "name$i $NUM (###)###-####" >> $file d...

Linux/perl mmap performance

I'm trying to optimize handling of large datasets using mmap. A dataset is in the gigabyte range. The idea was to mmap the whole file into memory, allowing multiple processes to work on the dataset concurrently (read-only). It isn't working as expected though. As a simple test I simply mmap the file (using perl's Sys::Mmap module, using...

Random Python dictionary key, weighted by values

hello, I have a dictionary where each key had a list of variable length, eg: d = { 'a': [1, 3, 2], 'b': [6], 'c': [0, 0] } Is there a clean way to get a random dictionary key, weighted by the length of its value? random.choice(d.keys()) will weight the keys equally, but in the case above I want 'a' to be returned roughly half the ...

How do I select a random element from an array in Python?

The first examples that I googled didn't work. This should be trivial, right? ...

Retain a random number across different functions in Cocoa?

I know how to do a global variable, but whenever I try to define a global variable with a random number function, xcode says "initializer element is not constant." The compiler doesn't want to make a variable from a random number because the random number function is not constant. How do I generate a random number and then use that s...

Checking if a graph is random using the Erdős–Rényi model?

Given some graph, I would like to determine how likely it is that it was generated randomly. I was told that a comparison to the Erdős–Rényi model was a good way to get this information, but I can't quite figure out how to do that. Any advice? ...

Multiple random number generator states in c/Unix

I'm using srandom() and random() to generate random numbers in c on a Unix system. I would like to have multiple RNGs. Each one, given the same seed, should output the same sequence. I would also like to save and restore the state of each one. Here's a pseudocode example: R1 = new_rng(5); //5 is the seed R2 = new rng(5); //5 is the ...

Random Number Between 2 Double Numbers

It is possible to generate a random number between 2 doubles? Example: public double GetRandomeNumber(double minimum, double maximum) { return Random.NextDouble(minimum, maximum) } Then I call it with the following: double result = GetRandomNumber(1.23, 5.34); Any thoughts would be appreciated. Thanks! ...

how to randomly choose 5 numbers out of 50 numbers in PHP?

Or is there a ready function there? ...

Object Attribute in Random List Not Accessible in Python

Hello. I'm working on my first object oriented bit of python and I have the following: #!/usr/bin/python import random class triangle: # Angle A To Angle C Connects Side F # Angle C to Angle B Connects Side D # Angle B to Angle A Connects Side E def __init__(self, a, b, c, d, e, f): self.a = a self.b = b self.c ...

Random list with rules

I'm trying to create a list of tasks that I've read from some text files and put them into lists. I want to create a master list of what I'm going to do through the day however I've got a few rules for this. One list has separate daily tasks that don't depend on the order they are completed. I call this list 'daily'. I've got anoth...

Random image display

I have a list of images that need to be displayed. However theres only space for 5. I need to display 5 of these at a time randomly. Whats the best way to do this? ...

How to generate random variable names in C++ using macros?

I'm creating a macro in C++ that declares a variable and assigns some value to it. Depending on how the macro is used, the second occurrence of the macro can override the value of the first variable. For instance: #define MY_MACRO int my_variable_[random-number-here] = getCurrentTime(); The other motivation to use that is to avoid sel...

Querying for N random records on Appengine datastore

Hi all, I'm trying to write a GQL query that returns N random records of a specific kind. My current implementation works but requires N calls to the datastore. I'd like to make it 1 call to the datastore if possible. I currently assign a random number to every kind that I put into the datastore. When I query for a random record I gen...

why do i always get the same sequence of random numbers with rand() ?

this is the first time i m trying random numbers with c (i miss c#) here is my code int i , j= 0; for(i=0;i<=10;i++){ j = rand(); printf("j = %d\n",j); } with this code i get the same sequance everytime the code but it generates random sequences if i add srand(/*somevalue/*) before the for loop . can someone explain why ? ...

PHP - Serialize floating points

Hi, I am generating 10 random floats between 6 and 8 (all for good reason), and writing them to a mysql database in a serialized form. But one quirk seems to emerge at the storage time: Before storing I'm just outputting the same data to see what it looks like, and this is the result I get a:10:{i:0;d:6.20000000000000017763568394002504...

How do I select a random key from an NSDictionary?

When I was using an NSArray, it was easy: NSArray *array = ... lastIndex = INT_MAX; ... int randomIndex; do { randomIndex = RANDOM_INT(0, [array count] - 1); } while (randomIndex == lastIndex); NSLog(@"%@", [array objectAtIndex:randomIndex]); lastIndex = randomIndex; I need to keep track of the lastIndex because I want the feeling o...