random number

How to generate integer random numbers (between 0 and 99)



0
tricubes (61)
1/15/2004 4:58:27 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
558 Views

Similar Articles

[PageSpeed] 40

"mosfet" <tricubes@wanadoo.fr> wrote:

>How to generate integer random numbers (between 0 and 99)

Look up rand() and srand(). You call srand() once to initialize the
generator, then each call to rand() returns a random integer between 0
and RAND_MAX. You could mod that result by 100 to get what you want.

-- 
Tim Slattery
MS MVP(DTS)
Slattery_T@bls.gov
0
Slattery_T (112)
1/15/2004 5:15:20 PM
"mosfet" <tricubes@wanadoo.fr> wrote in message
news:bu6gnj$mcb$1@news-reader3.wanadoo.fr...
> How to generate integer random numbers (between 0 and 99)

I saw this method sometime on another newsgroup. It assures that each number
has an equal chance of appearing (if the random generator were truely
random).

// generate random number between [0..n[
int rand_n(int n)
{
  if(n<=0 || n>RAND_MAX)
    return -1;
  int bucket_size = RAND_MAX / n;
  int r;
  do
    r = rand() / bucket_size;
  while( r >= n );
  return r;
}

int main()
{
    srand( (unsigned) time(NULL) );
    int r_n = rand_n( 100 );
}

Tom.


0
1/15/2004 10:59:42 PM
"Tim Slattery" <Slattery_T@bls.gov> wrote in message
news:giid00dip7mdnvm0salv1dic9dmeeqdge9@4ax.com...
> "mosfet" <tricubes@wanadoo.fr> wrote:
>
> >How to generate integer random numbers (between 0 and 99)
>
> Look up rand() and srand(). You call srand() once to initialize the
> generator, then each call to rand() returns a random integer between 0
> and RAND_MAX. You could mod that result by 100 to get what you want.

There is a problem with this approach: the numbers are not evenly
distributed:
http://groups.google.be/groups?threadm=f0070508.0309101537.7b180089%40posting.google.com

Tom.

> -- 
> Tim Slattery
> MS MVP(DTS)
> Slattery_T@bls.gov


0
1/16/2004 11:26:02 AM
Reply:

Similar Artilces:

Random
How to get random numbers random(0xff) ? int RandomNumber = (rand() * 0xff) / RAND_MAX; -- Bjarne Nielsen "]GHO[" <yousefk@taux01.nsc.com> skrev i en meddelelse news:0b6a01c3a84c$5733a890$a501280a@phx.gbl... > How to get random numbers random(0xff) ? rnd()%0x100; or rnd() & 0xff; Ruben On Tue, 11 Nov 2003 04:07:13 -0800, "]GHO[" <yousefk@taux01.nsc.com> wrote: >How to get random numbers random(0xff) ? ...

how would you calculate the number of hits to your website
Hi ,' can some one tell to me the answer of this question On a website, how would you calculate the number of hits to your website ...

Largest number in range
I am looking for a formula that will select the largest number in a range and make that number "bold." Is there a formula that will do this? Thanks in advance for any help. RBG Not a formula (formulas return values--they don't change formatting). But you could use Format|conditional formatting. If your range is B3:G9, then select that range and with B3 the activecell, then click on Format|conditional formatting Then Condition 1: Formula is and use this as the formula: =B3=MAX($B$3:$G$9) Excel will adjust the formula for all the cells in the selected area. (Don't...

Random Numbers not Random
When I create random numbers for an application with the following code, they do not appear to be truely random. Me.RandomNumber = Int(9 * Rnd + 1) ' Random Number 1-9 If I have two users setting side by side, they frequently get the same results when they start up Am I missing somthing? Any help would be appreciated -- Frank Wagner fwagner111@aol.com Frank, Have you tried a Randomize statement just before the RND? That should do it. -- hth Al Campagna Microsoft Access MVP http://home.comcast.net/~cccsolutions/index.html "Find a job tha...

Resequence figure numbers
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Using automated figure numbers in captions - have deleted a few, and the remaining ones do not automatically renumber. <br><br>How do I force the figure numbers to resequence? In Word 2007 on a Windows computer, it's just the F9 key. What works for Word 2008 on the Mac? Actually, it's F9 in Mac Word as well � or at least it *was* until Apple decided to expropriate that keystroke for Expose :-) You have several options available to you: 1- In System Preferences> Expose & Spaces...

Add numbers accross columns after stripping away text
I have the following data in a spreadsheet: A1 B1 C1 D1 E1 F1 4.5f 6f 3.5f 3f 7.25f I need to be able to add the numbers together to give me 24.25, i.e. strip the fs away. The numbers will always be less than 10 and the there will only ever be .25 or.5 or .75 after the number (I don't know whether that is significant?). If anyone can show me how to do this I would be very grateful. I have been messing around with MID and FIND to no avail and then started thinking that SUMPRODUCT might have to get involved but it all got a bit m...

Numbers matchup
I have a column of Account numbers in one worksheet. In another worksheet i have a column with account numbers and a column with SSN. I need a formula that will take the account number in the first worksheet and find it's match in the second worksheet and plug in the SSN on that same row as the Account number. Is this possible and how would be the best way to do this TIA Paul Have a look in HELP index for VLOOKUP -- Don Guillett SalesAid Software donaldb@281.com "Paul McGuire" <pmcguire@nottelling.com> wrote in message news:%2393selkAFHA.2932@TK2MSFTNGP10.phx.gbl....

Exchange server randomly asking random users to Authenticate randomly...
Has anyone had a problem like this where a user is in their Exchange mailbox using Outlook 2003 and the server prompts them for their credentials? If they hit cancel and exit out of Outlook and run it again it lets them into their mailbox. Thank you, Benjamin Pls make sure the outlook 2003 Client does not using Cache mode If is ,pls clearn this option -- Jammy "Benjamin Chan" <bchan[pleasenospam]@controlproductsinc.com> ���g��l��s�D :#BxdizrtEHA.3972@TK2MSFTNGP15.phx.gbl... > Has anyone had a problem like this where a user is in their Exchange mailbox > using Outlo...

unable to insert page numbers
my ms word 7 (enterprise package) has ceased to allow my to insert page numbers. Everythng else is working fine. I click "insert", that's ok; I click "page numbers" , that's ok; then I click "top of page" or "bottom, etc." and this is where my word 7 fails because no further option choice appear (though they had before). The only option to appear is "save selection as page number" and this is "grayed out" unless of course I have some current text selected. this is the same problem whether I attempt to insert ...

Generating Correlated Random Values in Excel
Hi Mike, Thanks so much for your advice. I think that I have it figured out, but can you confirm this for me? I can't afford to screw this up. If mean(inflation) = .031 stdev(inflation) = .047 mean(stock return) = .106 stdev(stock return) = .204 Column Headers: A B C D 1 Z1 Z2 Inflation Stock Return Data Generation Formulas A2 =NORMINV(RAND(),0,1) B2 =NORMINV(RAND(),0,1) C2 =0.031+0.047*A2 D2 =0.106+0.204*(A2*rho+B2*(1-rho^2)^0.5) Are these formulas correct to generate the random data? Particularly cell D2. Does this give me a complete value that t...

Macro to number blank cells
In one row I have: A B C D E F G Cust# Address 1 Address 2 Address3 Address 4 Address5 Address6 I have a macro that inserts 6 rows below each name, how could I copy information in columns B-G to the 6 cells below cust #, Thanks in advance for your help Fernando Can it be this simple? I'm not positive that I understand what you're asking. But try this. You have blank rows below each record. Select the columns B through G, beginning with the first BLANK row and down all rows. Hit Ed...

converting "text" numbers to numbers
I have been using the technique of multiplying cells that have number entries that behave like text entries by the value of 1 to convert them to numbers. However, some mixed cell entries, convert strangely. For example the entry "6 A" (without the quotes) becomes 0.25 when multiplied by 1. Any explanation? Any ideas on how to solve other than checking cell by cell to confirm all characters are numbers? Confused! Frustated! <grin>. Bob Just figured out the "why". An entry like 6 A is converted to 6AM. Its numerical value is 0.25. Still hoping for a ...

Addition problem, number always 2 cents off...Help
I have a little problem with my SUMs I have 200 to 300 rows of A * B = C Then I tell it to do a =sum(d1:d300) All the cells are formated to currency, two decimal places. when I add up the numbers with an adding machine I'm always 2 - 22 cents off. (2 cents on this sheet, 22 cents on another sheet....) Can anyone help Thank you WTG The issue is very likely to be rounding - that the product of each multiplication has fractional cents that Excel considers in the SUM() function, even though you don't see them fractional cents on screen You can either modify your multiplication f...

Auto Numbering
I want to be able to increase my ID field for each new record I enter, but I don't want to use an autonumber field in my table. I have used the following: ID = Nz(DMax("[id]", "tblNames")) + 1 and put it in the On Current property of the form. The problem is that when the last field is completed and the user presses Enter, the new record automatically puts in the new ID number. What I want to do is for the form to put in the next ID number but only when the 2nd field has data entered in it. Thanks Nigel, Place your code into the AfterUpdate event of the second ...

Sorting imported "numbers"
I've imported a long list of "numbers" from a server. Now in my excel table I can not sort them. I've tried multiplying the col. by "1" from paste special, Add from a free cell and paste special, using a macro form the converting text to number page, using trim and clean function, copy and paste as value, using the text to columns command, all to no avail. The "numbers" still stay left justified and sort by first number, i.e. 113 before 21 etc. There are 1500 entries and I do no think the table woudl be of much use after i tried to re-type in all t...

random pages
I have developed pages that randomly define a set of normal lung function data based on gender, age and height. I have applied formulas to this data simulate 5 classic lung function abnormalities (normal, restrictive, obstructive, mixed and early obstructive.) Currently the sheets that define the abnormal are named by one of the above lung abnormalities. I would like to present data from random pages in a work book to students and rather than have the pages named by the abnormality be named a simulated patient name. My questions. Can I have the name of a sheet set to equal data from to ...

Random Distribution
I need to randomly order numbers from a list or range of consecutive whole numbers, such that each number is listed only once. Can this be done in excel using functions, formulas or whatever? I want to incorporate it into a spreadsheet with a macro to automatically generate the list and use it to randomly designate selected entries. "Rick via OfficeKB.com" wrote: > I need to randomly order numbers from a list or range of consecutive whole > numbers, such that each number is listed only once. Can this be done in > excel using functions, formulas or whatever? I want to...

How to show what record number you are on in form?
I have created an unbound form in Access 2007 and am listing the records which are in my table. I found out how to show how many records there are in the table but would also like to show which record you are on. For example, when I click the next button to see the next record it will show: "Record 2 Of 30", and so on. This is the one I used to count the amount of records: Dim countRecords As Long countRecords = DCount("customerNumber", "customer") Me.txtRecordAmount = "Record # Of " & countRecords How can I show what reco...

Numbering Heading
Is there a way I can number heading and then continue with the same numbering level on the ensuing paragraphs, eg 1. First Heading 1.1 kdkdkdk 1.2 dldldldldl 2. Second Heading 2.1 ldldld 2.2 ldldldl -- Trainer See http://www.shaunakelly.com/word/numbering/OutlineNumbering.html for general principles. If you have Word 2007, the implementation is slightly different (through Define New Multilevel List), but the principles are the same. -- Suzanne S. Barnhill Microsoft MVP (Word) Words into Type Fairhope, Alabama USA http://word.mvps.org "Trainer&quo...

How to Retrieve Random Records through webservice method
Hi All, I'm writing an application that retrieve records from CRM 3.0 automatically using the web service. My question is, is there any way we can retrieve records randomly by using any webservice method? Like we can use RAND() or NEWID() to achieve this in SQL. Please help me if you have any idea about it. My sample method code is given below: CrmService service = new CrmService(); RetrieveMultipleRequest request2 = new RetrieveMultipleRequest(); // Create the ConditionExpression object. ConditionExpression condition = new ConditionExpression...

outline numbered list not auto-numbering
I have 2003 version of Word at work. I am trying to create an outline numbered list with 3 levels. When I hit the enter button after typing the text for an item, word does not seem to recognize that it should continue the list. The next line just has normal formatting, and I'm forced to use the format painter brush to copy a previous item from the list, and then Word seems to recognize it as part of the list. I have verified the "Apply Automatic Numbered Lists" is checked in the Tools/Autocorrect Options/Autoformat as you type menu. An observation about Word ...

How do I prevent duplicate numbers in a range of validated cells?
I have a validation rule for a series of cells. The 3 cells are only allowed to have a value of 3, 5, or 1. I want to fix it so that each number may only be used once in a range of 3 cells. .... So in A1:A3, each number may only appear once or an error message pops up or the cell starts blinking ~ something needs to happen to notify the user that there is an error that needs correcting. THanks ...

Random numbers
How exactly do i generate random numbers in VC++.Is the a function similar to random() in C++.Thanks in advance Fenn On Mon, 10 Jan 2005 22:42:52 -0800, "Fenn" <fenn_j@yahoo.com> wrote: >How exactly do i generate random numbers in VC++.Is the a function similar >to random() in C++.Thanks in advance > VC++i is C++. Besides, there is no random() in C++. I think you mean rand(). And besides, that is a C function imported into C++. In any event, generate random numbers in Visual C++ using MFC exactly the same way you would in any C++ (or C) program. .. yeah, b...

debugger shows wrong line number
I am experiencing a rather strange behavior -- when I step through my program, the debugger shows the debugging cursor at the wrong line. Also, when I set a breakpoint at any line, the debugger stops at a different line (sometimes the next line or the one after). Any idea why I might be seeing this problem. I am using Visual Studio .NET 2003 >I am experiencing a rather strange behavior -- when I step through my >program, the debugger shows the debugging cursor at the wrong line. > >Also, when I set a breakpoint at any line, the debugger stops at a different >line (someti...

Count down slide number
Is there a way to automatically generate a countdown slide counter as a footer on a presentation? I have been doing it manually to help me within time on a presentation. It sure would be a good feature to include in the program, IMHO. PowerPoint 2007 Terry, I am not sure what you mean by, “count down slide number”. If you mean slide numbers here’s how to do it:- 1. Open a new Presentation. 2. Insert tab / Text group / Header & Footer / Header and Footer window should launch / Slide tab / place a tick (check) in the Slide number field (towards the lower left hand...