473,385 Members | 1,838 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,385 software developers and data experts.

how to choose a random string of character in c++

1
like if i have a list of items and each time i want the program to choose one random item for me. i know how to generate a random number..but not picking a random string of charachters in a specific list..i don't know if i explain it right but i just want a general i dea about it ..
Dec 2 '09 #1
3 3987
Banfa
9,065 Expert Mod 8TB
Well just generate a random number N and then select the Nth string from you list.
Dec 2 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
A trick:

If you know the number of items in your list, say it is 100, then you random number has to be between 0 and 99. Then if you use a system provided random number generation function you can take the result % 100 to get that range 0 to 99.
Dec 2 '09 #3
Banfa
9,065 Expert Mod 8TB
The problem with that trick is that on some systems (hopefully fewer and fewer as time goes by) the numbers produced by the random number generator don't have very much randomness in the lower bits due to the algorithm used. Using % as you have specifically discards the high order bits and relies on the low order bits.

You get better randomness by using the whole of the range returned by rand.

This and other pitfalls of using rand are explained in C-FAQ Q13.16 How can I get random integers in a certain range?
Dec 2 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: vishal | last post by:
i want to generate a random number of a fixed length so how can i do this ?? i know some function which returns a single random character at a time but is there any built-in function which...
3
by: Jim Davidson | last post by:
Here's what I'm trying to do: I have a string with 30 numbers in it. I want to generate a random number between 0 an 30, go to that position in the string and get that number. Here is the code...
1
by: David Sworder | last post by:
Hi, Each instance of my client application will generate a 15 character random string the first time it is run. This unique string will be used to identify the client from that point forward....
6
by: lucy | last post by:
Hello comp.lang.js.I need a script to rotate a background image on each newpage load, given the id of the element and a list (array?)of 2 or more image paths ("img1.jpg", "img2.jpg", ...).Actually,...
10
by: Leon | last post by:
I know by default the random number generator use the time, but what is the best seed I can used in my web application? The Program generate 6 unique random numbers and load each of them in a...
24
by: Tuvas | last post by:
Okay, I'm working on devoloping a simple, cryptographically secure number, from a range of numbers (As one might do for finding large numbers, to test if they are prime). My function looks like...
9
by: Robert Mago | last post by:
Is there a way to create a 10 characthers or less, alph-numeric string which is unique. I can't use the guid since its longer then 10 characthers. Also i cannot use a random number, since being...
7
by: teh.sn1tch | last post by:
I created a random number generator for an application that uses a MersenneTwister class I found on the net. Basically I generate two random numbers from the MersenneTwister class and use each one...
9
by: JimmyJava694 | last post by:
Hey everyone, I need help trying to mutate a String. For example.. I ask the person to input a number. Then I ask them to input a String. (The string they input can be any length, but must...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.