473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Random-numbers...?

Sorry for this newbee question, but here goes...

I of course knows about the rand()-function, but are there
any functions that yields an integer randomnumber
between 0 (or 1) and a *programmer defined* number - e.g.
between 0 and 47 ? I tried redefining RAND_MAX but
that obviously wasn't the way to do it...

Alternatively a function returning a random float between
0 and 1 could be used...

As a last ditch, do anybody have a function that uses
rand() and RAND_MAX to "translate" rand()'s random-
numbers to any of the above... maybe also improving
the "randomness" ? I need to make random-numbers
with a value between 1 and 30-to-50, so it obviously
need to fairly random in those ranges...

-Koppe

Sep 21 '07 #1
7 1376
Koppe74 wrote:
Sorry for this newbee question, but here goes...
That's OK, but you should look at the FAQ - http://c-faq.com - before
asking newbie questions...
I of course knows about the rand()-function, but are there
any functions that yields an integer randomnumber
between 0 (or 1) and a *programmer defined* number - e.g.
between 0 and 47 ?
That's FAQ 13.6 - http://c-faq.com/lib/randrange.html
Sep 21 '07 #2
Koppe74 pisze:
Sorry for this newbee question, but here goes...

I of course knows about the rand()-function, but are there
any functions that yields an integer randomnumber
between 0 (or 1) and a *programmer defined* number - e.g.
between 0 and 47 ? I tried redefining RAND_MAX but
that obviously wasn't the way to do it...

RTFFAQ:

http://c-faq.com/lib/randrange.html
ch.
Sep 21 '07 #3
LPoD <ch****@pfff.irc.pl.wytnij.pfffwrites:
Koppe74 pisze:
>Sorry for this newbee question, but here goes...
I of course knows about the rand()-function, but are there
any functions that yields an integer randomnumber
between 0 (or 1) and a *programmer defined* number - e.g.
between 0 and 47 ? I tried redefining RAND_MAX but
that obviously wasn't the way to do it...

RTFFAQ:

http://c-faq.com/lib/randrange.html
RTFFAQ yourself (the first 'F' stands for "Fine", of course):

[Note to web authors, catalogers, and bookmarkers: the URL
<http://www.c-faq.com/is the right way to link to these
pages. All other URL's implementing this collection are subject to
change.]

When I cite the FAQ, I always post the base URL of the FAQ and the
relevant question number. This avoids problems if the URLs are
changed. It also encourages (well, forces) the reader to start at the
front page and navigate to the relevant question, and perhaps learn
something on the way.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 21 '07 #4
Koppe74 wrote:

[...]
As a last ditch, do anybody have a function that uses
rand() and RAND_MAX to "translate" rand()'s random-
numbers to any of the above... maybe also improving
the "randomness" ?
Well, rand() isn't a RNG, given the same seed, it will be very much
deterministic. :)

Improving a bad PRNG, by some private tricks, is usually a bad idea, you
are better off by using another PRNG.

True RNG (TRNG) are more interesting and are typically biased. A method
to unbias TRNG, lets consider a bit stream:

if pair of bits are equal, consider next pair
if pair of bits are different, emit previous pair.
--
Tor <torust [at] online [dot] no>
Sep 21 '07 #5
Tor Rustad wrote:

[...]
True RNG (TRNG) are more interesting and are typically biased. A method
to unbias TRNG, lets consider a bit stream:

if pair of bits are equal, consider next pair
if pair of bits are different, emit previous pair.
Ooops... change last line to:

if pair of bits are different, emit 1 or 0
--
Tor <torust [at] online [dot] no>
Sep 23 '07 #6
Tor Rustad wrote:
Tor Rustad wrote:
>True RNG (TRNG) are more interesting and are typically biased. A
method to unbias TRNG, lets consider a bit stream:

if pair of bits are equal, consider next pair
if pair of bits are different, emit previous pair.

Ooops... change last line to:

if pair of bits are different, emit 1 or 0
By "emit 1 or 0" do you mean "emit 1 or 0, randomly chosen"? ;-)

It would work to emit the second bit of the pair.
--
Thad
Sep 24 '07 #7
Thad Smith wrote:
Tor Rustad wrote:
>Tor Rustad wrote:
>>True RNG (TRNG) are more interesting and are typically biased. A
method to unbias TRNG, lets consider a bit stream:

if pair of bits are equal, consider next pair
if pair of bits are different, emit previous pair.

Ooops... change last line to:

if pair of bits are different, emit 1 or 0

By "emit 1 or 0" do you mean "emit 1 or 0, randomly chosen"? ;-)
LOL
It would work to emit the second bit of the pair.
Yes. Alternatively, emit the first bit of the pair. The technique is due
to von Neumann, since lots of bits will be discarded, more efficient
methods exists.

In practice, I think most implementations rather use a hashing scheme,
even if it isn't a provable method for un-skrewing a bit-sequence. Also,
there are methods based on Fast Fourier Transforms.
--
Tor <torust [at] online [dot] no>
Sep 24 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
1
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length...
3
by: TaTonka | last post by:
hi! how can i manage it (html or jscript with css) that everytime a user loads or refreshes a page, the page has a new bgcolor. i want to put it in a single file, so that all my pages have the...
15
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
14
by: DataSmash | last post by:
Hi, When I import the random module at the python interpreter, it works fine: >>> import random >>> x = random.randint(1,55) >>> print x 14 >>> BUT, when I put the same code in a python...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
6
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
by: Army1987 | last post by:
Is there anything wrong with this program? It seems to behave strangely if I give stdin EOF when asked for the character set... /* BEGIN pwdgen.c */ #include <stdio.h> #include "random.h"...
2
by: blaine | last post by:
Hey everyone, Just a friendly question about an efficient way to do this. I have a graph with nodes and edges (networkx is am amazing library, check it out!). I also have a lookup table with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.